【问题标题】:BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic,dynamic>BlocProvider.of() 使用不包含 Bloc<dynamic,dynamic> 类型的 Bloc 的上下文调用
【发布时间】:2020-05-20 00:18:32
【问题描述】:

错误:I/flutter (5919):══╡ 小部件库发现异常 ╞═════════════════════════════════════════════════ ══════════ I/颤振 (5919):在构建 Builder 时抛出了以下断言: I/flutter(5919):使用上下文调用 BlocProvider.of() 不包含 Bloc 类型的 Bloc。 I/flutter(5919):找不到祖先 从传递给 I/flutter ( 5919) 的上下文开始: BlocProvider.of>()。我/颤振(5919):
如果您使用的上下文来自 块提供者。 I/flutter(5919):使用的上下文是: BlocBuilder,动态>(脏,状态:I/flutter( 第5919章 dynamic>#55a7d(lifecycle state: created)) I/flutter (5919): The 相关的导致错误的小部件是:I/flutter(5919):MaterialApp /lib/main.dart:35:12

这是我的主要内容

void main() {
  final StorageRepository storageRepository = StorageRepository();
  final AuthenticationRepository authenticationRepository =
      AuthenticationRepository();
  runApp(BlocProvider<AuthenticationBloc>(
      create: (_) => AuthenticationBloc(
          authenticationRepository: authenticationRepository,
          storageRepository: storageRepository),
      child: MyApp()));
}

MaterialApp 小部件

MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.deepPurple),
      home: BlocBuilder(
        builder: (context, state) {
          print(state);
          if (state is Authenticated) {
            return MainPage();
          } else if (state is Unauthenticated) {
            return LoginPage();
          } else if (state is Uninitialized) {
            return SplashScreen();
          }

          return Container();
        },
      ),

【问题讨论】:

    标签: flutter bloc flutter-bloc


    【解决方案1】:

    您忘记将 Bloc 和 State 类型赋予 BlocBuilder 小部件

    MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(primarySwatch: Colors.deepPurple),
          /// You need to specify the type here, 
          /// that's why you got error Bloc<dynamic, dynamic>
          home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
            builder: (context, state) {
              print(state);
              if (state is Authenticated) {
                return MainPage();
              } else if (state is Unauthenticated) {
                return LoginPage();
              } else if (state is Uninitialized) {
                return SplashScreen();
              }
    
              return Container();
            },
          ),
    

    【讨论】:

      【解决方案2】:

      作为错误,本身提示BlocProvider没有访问正确的context来使用bloc

      MultiBlocProvider 提供了添加多个提供者的能力,然后这些提供者可以获得正确的上下文访问,因为MultiBlocProvider 将BlocProvider 列表转换为嵌套树 BlocProvider 小部件。

      MultiBlocProvider(
                providers: [
                  BlocProvider<YourBloc>(
                      create: (BuildContext context) =>)
                ],
                child: MaterialApp(
                  home: BlocBuilder<YourBloc, YourState>(
      

      【讨论】:

        猜你喜欢
        • 2021-02-08
        • 2020-08-21
        • 2020-10-25
        • 2020-12-26
        • 2020-10-16
        • 2020-11-15
        • 2021-06-10
        • 2020-01-25
        • 2019-09-29
        相关资源
        最近更新 更多