在Flutter中导航时未获取提供者数据

时间:2020-10-13 08:54:58

标签: flutter flutter-provider

我知道flutter提供程序是作用域。因此,我在MaterialApp的顶部声明了提供程序(到处都需要这些提供程序)。在一个屏幕中,更改提供者值并导航到另一个屏幕。在该屏幕上没有获取数据。需要建议和指导我做错了地方

main.dart

runApp(
 MultiProvider(
  providers: [
    ChangeNotifierProvider<UserAuthViewModel>(
        create: (context) => sl<UserAuthViewModel>()),
    ChangeNotifierProvider<UserProfileViewModel>(
        create: (context) => sl<UserProfileViewModel>()),
    ChangeNotifierProvider<BottomNavViewModel>(
        create: (context) => sl<BottomNavViewModel>()),
  ],
  child: MaterialApp(
    title: "Footsapp",
    theme: ThemeData(fontFamily: 'Montserrat'),
    debugShowCheckedModeBanner: false,
    home: isFirstLaunch == true ? OnBoarding() : SplashView(),
    routes: {
      //onboarding
      SplashView.SCREEN_ID: (context) => SplashView(),

      //bottom nav pages
      BottomNavContainer.SCREEN_ID: (context) => BottomNavContainer(),
    },
  ),
),
);

启动屏幕,可从api调用中获取一些信息

class _SplashViewState extends State<SplashView>
with TokenProvider, AfterLayoutMixin<SplashView> {

final userProfileViewModel = sl<UserProfileViewModel>();
final prefUtil = sl<SharedPrefUtil>();

@override
void afterFirstLayout(BuildContext context) {
 Future.delayed(Duration(seconds: 1), () async {
  bool isLoggedIn = prefUtil.readBool(IS_LOGGED_IN) ?? false;
  bool initialProfileUpdated =
      prefUtil.readBool(INITIAL_PROFILE_UPDATED) ?? false;
  isLoggedIn == true
      ? getProfileInfo()
      : await Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(
            builder: (_) => SocialLoginRegScreen(),
          ),
          (route) => false);
});
}

@override
void initState() {
 super.initState();
}

void getProfileInfo() async {
  final userProfileResponse = await userProfileViewModel.getUserProfileData();

if (userProfileResponse.success == true) {
  print('In splash: ${userProfileViewModel.userProfile.toString()}');
  //from log
  In splash: {firstName: Ashif, lastName: 123, birthday: 1990-02-03, email: 
  ashif123@gmail.com, preferredFoot: Left, preferredPosition: Midfielder, 
  numberOfTimesPlayedWeekly: 4}

  Future.delayed(Duration(milliseconds: 500), () async {
    await Navigator.pushReplacementNamed(
      context,
      BottomNavContainer.SCREEN_ID,
    );
  });
}
}

提供商模型类

class UserProfileViewModel extends BaseViewModel {
 final _profileManageRepo = sl<ProfileManageRepo>();

 Profile userProfile = Profile.initial();

Future<UserDataResponse> getUserProfileData() async {
 final _userDataResponse = await _profileManageRepo.getUserProfileInfo();

if (_userDataResponse.success == true) {
  userProfile = _userDataResponse.profile;
} else {
  setState(ViewState.Error);
  errorMessage = 'Please try again!';
}

 return _userDataResponse;
}

正在尝试从配置文件屏幕获取提供者数据(用户配置文件)。但是它总是会得到初始值。

class _UserProfileScreenState extends State<UserProfileScreen> {


@override
void initState() {
 super.initState();
}

@override
Widget build(BuildContext context) {
 return Consumer<UserProfileViewModel>(
  builder: (context, viewmodel, child) {
    print('In profile: ${viewmodel.userProfile.toString()}');
    //but here 
    In profile: {firstName: , lastName: , birthday: , email: , preferredFoot: 
    Left, preferredPosition: , numberOfTimesPlayedWeekly: 1(default value)}
    return Container(
      child: ProfileCardWidget(
        profile: viewmodel.userProfile,
      ),
    );
  },
 );
 }
}

0 个答案:

没有答案