【问题标题】:MultiRepositoryProvider doesn't instantiate BlocMultiRepositoryProvider 不实例化 Bloc
【发布时间】:2021-02-01 22:03:05
【问题描述】:

我最近开始在 Flutter 中开发一个应用程序,所以我对这个领域还很陌生。所以我一直在研究使用 Blocs。但是,当我实例化我的 Bloc 和我的服务时,一切正常。也就是说,直到我使用MultiRepositoryProvider。我有 2 个代码 sn-ps。第一个:

return RepositoryProvider<AuthenticationService>(
      create: (context) {
        return FakeAuthenticationService();
      },
      // Injects the Authentication BLoC
      child: BlocProvider<AuthenticationBloc>(
        create: (context) {
          final authService = RepositoryProvider.of<AuthenticationService>(context);
          return AuthenticationBloc(authService)..add(AppLoaded());
        },
        child:  MaterialApp(
          title: 'Authentication Demo',
          theme: appTheme(),
          home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
            builder: (context, state) {
              if (state is AuthenticationAuthenticated) {
                // show home page
                return HomePage(
                  user: state.user,
                );
              }
              // otherwise show login page
              return StartupPage();
            },
          ),
        )
      ),
    );

这段代码工作正常,但第二个 sn-p 完全相同,除了它使用MultiRepositoryProvider 不起作用。第二个代码:

return MultiRepositoryProvider(
      providers: [
        RepositoryProvider<AuthenticationService>(
          create: (context) => FakeAuthenticationService(),
          child: BlocProvider<AuthenticationBloc>(
            create: (context) {
              final authService = RepositoryProvider.of<AuthenticationService>(context);
              return AuthenticationBloc(authService)..add(AppLoaded());
            },
          ),
        )
      ],
      child: MaterialApp(
        title: 'Authentication Demo',
        theme: appTheme(),
        home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
          builder: (context, state) {
            if (state is AuthenticationAuthenticated) {
              // show home page
              return HomePage(
                user: state.user,
              );
            }
            // otherwise show login page
            return StartupPage();
          },
        ),
      ),
    );

现在第二个代码给了我错误BlocProvider.of() called with a context that does not contain a Cubit of type AuthenticationBloc.

有谁知道为什么第二个代码不起作用?

【问题讨论】:

    标签: flutter repository-pattern flutter-bloc


    【解决方案1】:

    我正在做同样的事情,我遇到了一个错误,但现在解决了

    return MultiRepositoryProvider(
        providers: [
          RepositoryProvider<TranslationRepository>(
            create: (context) => TranslationRepository(),
          ),
          RepositoryProvider<WeatherRepository>(
            create: (context) => WeatherRepository(),
          ),
        ],
        child: MultiBlocProvider(
            providers: [
              BlocProvider<WeatherBloc>(
                create: (context) =>
                    WeatherBloc(context.read<WeatherRepository>()),
              ),
              BlocProvider<ConnectivityBloc>(
                create: (context) => ConnectivityBloc(),
              ),
              BlocProvider<TranslationBloc>(
                create: (context) =>
                    TranslationBloc(context.read<TranslationRepository>()),
              ),
            ],
            child: MaterialApp(
              title: 'Material App',
              onGenerateRoute: router.generateRoute,
              initialRoute: '/',
            )));
    

    首先,在我的创建函数中,我用“_”覆盖了上下文,但我得到了同样的错误。 现在有了这个 sn-p,它就可以完美地工作了,只需在之前输入与我的提供程序相同的上下文名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-07
      • 2019-11-03
      • 1970-01-01
      • 2021-01-31
      • 2019-10-16
      • 2020-10-06
      • 2021-02-23
      • 2015-09-11
      相关资源
      最近更新 更多