Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振:在同一树中使用Multiprovider和Consumer的依赖注入_Android_Flutter_Dependency Injection_Flutter Provider - Fatal编程技术网

Android 颤振:在同一树中使用Multiprovider和Consumer的依赖注入

Android 颤振:在同一树中使用Multiprovider和Consumer的依赖注入,android,flutter,dependency-injection,flutter-provider,Android,Flutter,Dependency Injection,Flutter Provider,我试图将在同一树级别创建的服务实例注入到另一个提供者中。但是在访问提供者时,我得到了ProviderNotFoundException。 在以下代码中,NotificationService依赖于AuthService。它需要在构造函数中传递。因此,我使用文档中提到的Consumer和Provider.value注入它: 以下是伪代码: return MultiProvider( providers: [ Provider<AuthService>(

我试图将在同一树级别创建的服务实例注入到另一个提供者中。但是在访问提供者时,我得到了ProviderNotFoundException。 在以下代码中,NotificationService依赖于AuthService。它需要在构造函数中传递。因此,我使用文档中提到的Consumer和Provider.value注入它:

以下是伪代码:

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()
);
错误:

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的方式无效。但实际上你并不需要消费者+提供者。你可以做:

多供应商 供应商:[ Providercreate:=>A, Providercreate:context=>BProvider.ofcontext,listen:false, ], 您使用Provider.value的方式无效。但实际上你并不需要消费者+提供者。你可以做:

多供应商 供应商:[ Providercreate:=>A, Providercreate:context=>BProvider.ofcontext,listen:false, ],
非常感谢。这就解决了问题。我浏览了api文档:。但我无法真正理解这个解释,listen:false到底做了什么?@dora查看我的文章,以便更好地理解listen标志。它还引用了文档。@Rémi如果要在提供程序A更新时更新提供程序B,那么我们需要为B使用代理提供程序。如果我错了,请更正。谢谢。这就解决了问题。我浏览了api文档:。但我无法真正理解这个解释,listen:false到底做了什么?@dora查看我的文章,以便更好地理解listen标志。它还引用了文档。@Rémi如果要在提供程序A更新时更新提供程序B,那么我们需要为B使用代理提供程序。如果我错了,请纠正我。
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):