Flutter:在同一棵树中使用 Multiprovider 和 Consumer 进行依赖注入
Flutter: Dependency Injecting using Multiprovider and Consumer in the same tree
我正在尝试将服务实例(已在同一树级别创建)注入另一个提供者。但是在访问提供者时沿着树向下,我得到 ProviderNotFoundException
异常。
在下面的代码中 NotificationService
依赖于 AuthService
。哪个需要在构造函数中传递。因此,我使用 Consumer
和 Provider.value
注入它,如文档中所述:https://pub.dev/documentation/provider/latest/provider/Consumer-class.html
这是伪代码:
return MultiProvider(
providers: [
Provider<AuthService>(
create: (ctx) => AuthService(_storage),
dispose: (ctx, v) => v.dispose(),
),
Consumer<AuthService>(
builder: (context, v, child) {
return Provider.value(
value: Provider<NotificationService>(
create: (ctx) => NotificationService(v),
dispose: (ctx, v) => v.dispose(),
),
child: child
);
},
)
],
child: MyApp()
);
树线下的某个地方,当试图访问 NotificationService
实例时,我得到 ProviderNotFoundException
:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final NotificationService _ns = Provider.of<NotificationService>(context);
}
}
错误:
I/flutter ( 4614): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4614): The following ProviderNotFoundException was thrown building MyApp(dirty, dependencies:
I/flutter ( 4614): [_DefaultInheritedProviderScope<AuthService>]):
I/flutter ( 4614): Error: Could not find the correct Provider<NotificationService> above this MyApp Widget
I/flutter ( 4614):
I/flutter ( 4614): To fix, please:
I/flutter ( 4614):
I/flutter ( 4614): * Ensure the Provider<NotificationService> is an ancestor to this MyApp Widget
I/flutter ( 4614): * Provide types to Provider<NotificationService>
I/flutter ( 4614): * Provide types to Consumer<NotificationService>
I/flutter ( 4614): * Provide types to Provider.of<NotificationService>()
I/flutter ( 4614): * Ensure the correct `context` is being used.
I/flutter ( 4614):
我不是很明白,我很确定上面的代码有错误。我究竟做错了什么?
您使用Provider.value
的方式无效。但你实际上并不需要Consumer
+Provider
。你可以这样做:
MultiProvider(
providers: [
Provider(create: (_) => A()),
Provider(create: (context) => B(Provider.of<A>(context, listen: false)),
],
)
我正在尝试将服务实例(已在同一树级别创建)注入另一个提供者。但是在访问提供者时沿着树向下,我得到 ProviderNotFoundException
异常。
在下面的代码中 NotificationService
依赖于 AuthService
。哪个需要在构造函数中传递。因此,我使用 Consumer
和 Provider.value
注入它,如文档中所述:https://pub.dev/documentation/provider/latest/provider/Consumer-class.html
这是伪代码:
return MultiProvider(
providers: [
Provider<AuthService>(
create: (ctx) => AuthService(_storage),
dispose: (ctx, v) => v.dispose(),
),
Consumer<AuthService>(
builder: (context, v, child) {
return Provider.value(
value: Provider<NotificationService>(
create: (ctx) => NotificationService(v),
dispose: (ctx, v) => v.dispose(),
),
child: child
);
},
)
],
child: MyApp()
);
树线下的某个地方,当试图访问 NotificationService
实例时,我得到 ProviderNotFoundException
:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final NotificationService _ns = Provider.of<NotificationService>(context);
}
}
错误:
I/flutter ( 4614): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4614): The following ProviderNotFoundException was thrown building MyApp(dirty, dependencies:
I/flutter ( 4614): [_DefaultInheritedProviderScope<AuthService>]):
I/flutter ( 4614): Error: Could not find the correct Provider<NotificationService> above this MyApp Widget
I/flutter ( 4614):
I/flutter ( 4614): To fix, please:
I/flutter ( 4614):
I/flutter ( 4614): * Ensure the Provider<NotificationService> is an ancestor to this MyApp Widget
I/flutter ( 4614): * Provide types to Provider<NotificationService>
I/flutter ( 4614): * Provide types to Consumer<NotificationService>
I/flutter ( 4614): * Provide types to Provider.of<NotificationService>()
I/flutter ( 4614): * Ensure the correct `context` is being used.
I/flutter ( 4614):
我不是很明白,我很确定上面的代码有错误。我究竟做错了什么?
您使用Provider.value
的方式无效。但你实际上并不需要Consumer
+Provider
。你可以这样做:
MultiProvider(
providers: [
Provider(create: (_) => A()),
Provider(create: (context) => B(Provider.of<A>(context, listen: false)),
],
)