简体   繁体   English

Flutter 等待 Provider 在 multiprovider 中准备就绪

[英]Flutter await Provider to be ready inside multiprovider

I have a problem with synchronize Providers creation.我在同步提供者创建时遇到问题。 I'm new to flutter, I'll try to explain better as i can.我是颤振的新手,我会尽量解释得更好。

In my main.dart i have two providers one for the user auth and one for another widget into the code, it simply have a list to display.在我的 main.dart 中,我有两个提供程序,一个用于用户身份验证,另一个用于代码中的另一个小部件,它只需要显示一个列表。 I use ChangeNotifierProxyProvider because all other provider needs to access to user auth tokens and details.我使用 ChangeNotifierProxyProvider 因为所有其他提供者都需要访问用户身份验证令牌和详细信息。 All methods for user to store,read tokens are in the userProvider.用户存储、读取令牌的所有方法都在 userProvider 中。

When UserProvder.init() is called the object is created but is still not ready beacuse of the http request, the code in the main continue to execute and pass the UserProvider to the Conto Provider that thinks UserProvider is ready but it is not.当调用 UserProvder.init() 时,对象已创建但由于 http 请求仍未准备好,主程序中的代码继续执行并将 UserProvider 传递给认为 UserProvider 已准备好但尚未准备好的 Conto Provider。 When the ContoProvider start to retrive the List with the token inside UserProvider access through userService failed the call because is still null.当 ContoProvider 开始使用 UserProvider 内部的令牌检索 List 时,通过 userService 访问失败,因为它仍然为空。

So, How can i synchronize the creation of provider in order to wait the UserProvider to be full ready and then initialize all other providers?那么,我如何同步提供者的创建以等待 UserProvider 完全准备好然后初始化所有其他提供者?

main.dart main.dart

Widget build(BuildContext context) {
return MultiProvider(
  providers: [
    ChangeNotifierProvider<UserProvider>(
      create:  (_)  => UserProvider.init(),

    ),
    ChangeNotifierProxyProvider<UserProvider, ContoProvider>(
      create: (_) {
        return ContoProvider.init(
            Provider.of<UserProvider>(_, listen: false));
        },

      update: (_,  userProvider,  contoProvider) =>
          contoProvider..update(userProvider),
    ),
  ],
  child:... 

userProvider.dart userProvider.dart

      User activeUser = User(null, null, null);
      UserStatus status = UserStatus.Checking;


        UserProvider.init() {

            checkUserPresence();
          }

          void checkUserPresence() async {
             /*here i check if in secure storage there is a refreshtoken if there is i make an http
 request for a new token and a second request to fill the User into UserProvider so i need await async*/
          }

ContoProvider.dart ContoProvider.dart

    UserProvider userService;
      ContoProvider.init(UserProvider user) {

        userService = user;

        lookUpConti();
      }

      void lookUpConti() async {
        /*here i make an http call to retrive some data, i access to 
userService for get token and refresh token if needed*/
    }

You can use WidgetsFlutterBinding.ensureInitialized()您可以使用WidgetsFlutterBinding.ensureInitialized()

void main() {

  /** WidgetsFlutterBinding.ensureInitialized() is required in Flutter v1.9.4+ before using any plugins if the code is executed before runApp. */

  WidgetsFlutterBinding.ensureInitialized();



  runApp(
    MultiProvider(
      providers: [
         ChangeNotifierProvider<UserProvider>(
           create:  (_)  => UserProvider.init()),

         ChangeNotifierProxyProvider<UserProvider, ContoProvider>(
            create: (_) {
               return ContoProvider.init(
                   Provider.of<UserProvider>(_, listen: false));
             },

           update: (_,  userProvider,  contoProvider) =>
             contoProvider..update(userProvider),
         ),
       ],
      child: MyApp(),
    ),

}

PS : I will recommend that you separate your repositories from your provider. PS :我建议您将存储库与提供商分开。 That is, not API call to external/web resources should be found in your provider.也就是说,不应在您的提供者中找到对外部/网络资源的 API 调用。 You can pass such class into your provider as arguments.您可以将此类类作为参数传递给您的提供者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM