错误:I / flutter(5919):══╡小组件库异常提示 ╞═════════════════════════════════════════════════ ══════════我/扑 (5919):构建Builder时引发了以下断言: I / flutter(5919):使用上下文调用BlocProvider.of() 不包含Bloc类型的Bloc。 I / flutter(5919):找不到祖先 从传递给I / flutter(5919)的上下文开始: BlocProvider.of>()。 I / flutter(5919):
如果您使用的上下文来自上方的小部件,则可能会发生这种情况 BlocProvider。 I / flutter(5919):使用的上下文是: BlocBuilder,动态>(脏,状态:I / flutter( 5919):_BlocBuilderBaseState, 动态>#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小部件
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();
},
),
答案 0 :(得分:3)
您忘记将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();
},
),
答案 1 :(得分:0)
作为错误,本身建议 BlocProvider
未访问正确的 context
以使用该集团
MultiBlocProvider
提供了添加多个提供者的能力,这些提供者然后可以获得正确的上下文访问,因为 MultiBlocProvider
将 BlocProvider
列表转换为嵌套的树
BlocProvider
小部件。
MultiBlocProvider(
providers: [
BlocProvider<YourBloc>(
create: (BuildContext context) =>)
],
child: MaterialApp(
home: BlocBuilder<YourBloc, YourState>(