BlocProvider.of () вызывается с контекстом, который не содержит Blo c типа Blo c - PullRequest
1 голос
/ 04 февраля 2020

Ошибка: I / трепетание (5919): ══╡ ИСКЛЮЧЕНИЕ ВЫСТАВЛЕНО БИБЛИОТЕКОЙ ВИДЖЕТОВ ═════════════════════════════════ I / трепетание (5919): Было выброшено следующее утверждение. Строитель: I / flutter (5919): BlocProvider.of () вызывается с контекстом, который не содержит Blo c типа Blo c. I / flutter (5919): не может быть найдено предка, начиная с контекста, который был передан I / flutter (5919): BlocProvider.of> (). I / flutter (5919):
Это может произойти, если используемый вами контекст происходит из виджета над BlocProvider. I / flutter (5919): использовался контекст: BlocBuilder, Dynami c> (грязный, состояние: I / Flutter (5919): _BlocBuilderBaseState, Dynami c> # 55a7d (состояние жизненного цикла: создан)) I / Flutter (5919): соответствующий виджет, вызывающий ошибки: 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 Widget

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();
        },
      ),

1 Ответ

1 голос
/ 04 февраля 2020

Вы забыли предоставить Blo c и тип 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();
        },
      ),
...