BlocProvider.of() 使用不包含 Bloc<dynamic,dynamic> 类型的 Bloc 的上下文调用
BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic,dynamic>
Error: I/flutter ( 5919): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
( 5919): The following assertion was thrown building Builder:
I/flutter ( 5919): BlocProvider.of() called with a context
that does not contain a Bloc of type Bloc. I/flutter ( 5919): No ancestor could be found
starting from the context that was passed to I/flutter ( 5919):
BlocProvider.of>(). I/flutter ( 5919):
This can happen if the context you used comes from a widget above the
BlocProvider. I/flutter ( 5919): The context used was:
BlocBuilder, dynamic>(dirty, state: I/flutter (
5919): _BlocBuilderBaseState,
dynamic>#55a7d(lifecycle state: created)) I/flutter ( 5919): The
relevant error-causing widget was: 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();
},
),
您忘记为 BlocBuilder 小部件提供 Bloc 和 State 类型
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();
},
),
作为错误,本身就提示BlocProvider
没有访问权限context
使用bloc
MultiBlocProvider
提供了添加多个提供者的能力,这样就可以获得正确的上下文访问,因为 MultiBlocProvider
将 BlocProvider
列表转换为嵌套的树
BlocProvider
个小部件。
MultiBlocProvider(
providers: [
BlocProvider<YourBloc>(
create: (BuildContext context) =>)
],
child: MaterialApp(
home: BlocBuilder<YourBloc, YourState>(
Error: I/flutter ( 5919): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 5919): The following assertion was thrown building Builder: I/flutter ( 5919): BlocProvider.of() called with a context that does not contain a Bloc of type Bloc. I/flutter ( 5919): No ancestor could be found starting from the context that was passed to I/flutter ( 5919): BlocProvider.of>(). I/flutter ( 5919):
This can happen if the context you used comes from a widget above the BlocProvider. I/flutter ( 5919): The context used was: BlocBuilder, dynamic>(dirty, state: I/flutter ( 5919): _BlocBuilderBaseState, dynamic>#55a7d(lifecycle state: created)) I/flutter ( 5919): The relevant error-causing widget was: 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();
},
),
您忘记为 BlocBuilder 小部件提供 Bloc 和 State 类型
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();
},
),
作为错误,本身就提示BlocProvider
没有访问权限context
使用bloc
MultiBlocProvider
提供了添加多个提供者的能力,这样就可以获得正确的上下文访问,因为 MultiBlocProvider
将 BlocProvider
列表转换为嵌套的树
BlocProvider
个小部件。
MultiBlocProvider(
providers: [
BlocProvider<YourBloc>(
create: (BuildContext context) =>)
],
child: MaterialApp(
home: BlocBuilder<YourBloc, YourState>(