gpt4 book ai didi

flutter - BlocProvider.of() 使用不包含 Bloc 类型的 Bloc 的上下文调用

转载 作者:行者123 更新时间:2023-12-03 23:49:16 26 4
gpt4 key购买 nike

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

关于flutter - BlocProvider.of() 使用不包含 Bloc<dynamic,dynamic> 类型的 Bloc 的上下文调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60052844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com