我最近开始在 Flutter 中开发一个应用程序,所以我对该领域相当陌生。所以我一直在研究使用 Blocs。然而,当我实例化我的 Bloc 和我的服务时,一切正常。也就是说,直到我使用
MultiRepositoryProvider
。我有 2 个代码片段。第一个:
return RepositoryProvider<AuthenticationService>(
create: (context) {
return FakeAuthenticationService();
},
// Injects the Authentication BLoC
child: BlocProvider<AuthenticationBloc>(
create: (context) {
final authService = RepositoryProvider.of<AuthenticationService>(context);
return AuthenticationBloc(authService)..add(AppLoaded());
},
child: MaterialApp(
title: 'Authentication Demo',
theme: appTheme(),
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
// show home page
return HomePage(
user: state.user,
);
}
// otherwise show login page
return StartupPage();
},
),
)
),
);
这段代码工作正常,但第二个代码片段完全相同,只是它使用了
MultiRepositoryProvider
不起作用。第二个代码:
return MultiRepositoryProvider(
providers: [
RepositoryProvider<AuthenticationService>(
create: (context) => FakeAuthenticationService(),
child: BlocProvider<AuthenticationBloc>(
create: (context) {
final authService = RepositoryProvider.of<AuthenticationService>(context);
return AuthenticationBloc(authService)..add(AppLoaded());
},
),
)
],
child: MaterialApp(
title: 'Authentication Demo',
theme: appTheme(),
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
// show home page
return HomePage(
user: state.user,
);
}
// otherwise show login page
return StartupPage();
},
),
),
);
现在第二个代码给了我错误
BlocProvider.of() called with a context that does not contain a Cubit of type AuthenticationBloc.
有谁知道为什么第二个代码不起作用?
我正在做同样的事情,但遇到了错误,但现在已经解决了
return MultiRepositoryProvider(
providers: [
RepositoryProvider<TranslationRepository>(
create: (context) => TranslationRepository(),
),
RepositoryProvider<WeatherRepository>(
create: (context) => WeatherRepository(),
),
],
child: MultiBlocProvider(
providers: [
BlocProvider<WeatherBloc>(
create: (context) =>
WeatherBloc(context.read<WeatherRepository>()),
),
BlocProvider<ConnectivityBloc>(
create: (context) => ConnectivityBloc(),
),
BlocProvider<TranslationBloc>(
create: (context) =>
TranslationBloc(context.read<TranslationRepository>()),
),
],
child: MaterialApp(
title: 'Material App',
onGenerateRoute: router.generateRoute,
initialRoute: '/',
)));
首先,在我的创建函数中,我用“_”覆盖了上下文,但我得到了同样的错误。 现在有了这个片段,它可以完美工作,只需输入与我之前的提供者相同的上下文名称即可
错误:无法在此 AppWrapper 小部件上方找到正确的提供程序
我收到此错误,指出在当前路径或上下文中找不到提供程序。现在下面的代码可以正常工作了。我所做的唯一更改是在 Providers 的 create 方法中编写“context”而不是“_”:
将这些行从 create: (_) 更改为 create: (context)
正确代码
class AppWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiRepositoryProvider(
providers: [RepositoryProvider<AuthRepository>(create: (_) => AuthRepository())],
child: MaterialApp(
title: 'Pokedex App',
theme: themeData,
home: MultiBlocProvider(
providers: [
BlocProvider<AuthBloc>(
create: (_) => AuthBloc(
authRepository: context.read<AuthRepository>(),
),
),
BlocProvider(
create: (_) => LoginCubit(
authRepository: context.read<AuthRepository>(),
),
),
BlocProvider(
create: (_) => SignupCubit(
authRepository: context.read<AuthRepository>(),
),
),
],
child: SignupScreen(),
),
),
);
}
}