我想在我的flutter项目中使用提供程序包(4.0),但收到“尝试使用具有可侦听/流子类型的提供程序”错误/警告。
我的安抚:
我有一个Api()
类,其中包含用于firebase-cloudstorage的基本CRUD功能。
我有一个EventService()
类,它使用带有特定参数的Api()
类。
对于我来说,我有一个名为EventOverviewModel
的ViewModel类,该类使用EventService。
我的提供程序配置如下:
List<SingleChildWidget> providers = [
...independentServices,
...dependentServices,
...uiConsumableProviders,
];
// Services die unabhängig von anderen sind
List<SingleChildWidget> independentServices = [
Provider<EventService>(create: (_) => EventService(api: Api('events')))
];
// Services die von anderen Services abhängig sind
List<SingleChildWidget> dependentServices = [
ProxyProvider<EventService, EventOverviewModel>(
update: (_, srvc, __) => EventOverviewModel(eventService: srvc),
)
];
List<SingleChildWidget> uiConsumableProviders = [];
一切似乎都可以在应用程序中正常工作,但是无论如何,我都得到了“尝试使用具有可侦听/流子类型的提供程序”的信息。
我应该如何更改代码以不再收到此错误消息?
Running Gradle task 'assembleDebug'...
I/flutter (16553): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (16553): The following assertion was thrown building NumericProxyProvider<EventService, Void, Void, Void,
I/flutter (16553): Void, Void, EventOverviewModel>(dirty, dependencies: [InheritedProvider<EventService>], state:
I/flutter (16553): _ProxyProviderState<EventOverviewModel>#8d38d):
I/flutter (16553): Tried to use Provider with a subtype of Listenable/Stream (EventOverviewModel).
I/flutter (16553):
I/flutter (16553): This is likely a mistake, as Provider will not automatically update dependents
I/flutter (16553): when EventOverviewModel is updated. Instead, consider changing Provider for more specific
I/flutter (16553): implementation that handles the update mechanism, such as:
I/flutter (16553):
I/flutter (16553): - ListenableProvider
I/flutter (16553): - ChangeNotifierProvider
I/flutter (16553): - ValueListenableProvider
I/flutter (16553): - StreamProvider
I/flutter (16553):
I/flutter (16553): Alternatively, if you are making your own provider, consider using InheritedProvider.
I/flutter (16553):
I/flutter (16553): If you think that this is not an error, you can disable this check by setting
I/flutter (16553): Provider.debugCheckInvalidValueType to `null` in your main file:
I/flutter (16553):
I/flutter (16553): ```
I/flutter (16553): void main() {
I/flutter (16553): Provider.debugCheckInvalidValueType = null;
I/flutter (16553):
I/flutter (16553): runApp(MyApp());
I/flutter (16553): }
I/flutter (16553): ```
I/flutter (16553):
I/flutter (16553):
I/flutter (16553): The relevant error-causing widget was:
I/flutter (16553): NumericProxyProvider<EventService, Void, Void, Void, Void, Void, EventOverviewModel>
I/flutter (16553): file:///E:/Dev/flutter/.pub-cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/proxy_provider.dart:232:12
I/flutter (16553):
I/flutter (16553): When the exception was thrown, this was the stack:
I/flutter (16553): #0 Provider.debugCheckInvalidValueType.<anonymous closure>.<anonymous ...
答案 0 :(得分:14)
尝试更改
List<SingleChildWidget> independentServices = [
Provider<EventService>(create: (_) => EventService(api: Api('events')))
];
收件人:
List<SingleChildWidget> independentServices = [
ListenableProvider<EventService>(create: (_) => EventService(api: Api('events')))
];
和
List<SingleChildWidget> dependentServices = [
ProxyProvider<EventService, EventOverviewModel>(
update: (_, srvc, __) => EventOverviewModel(eventService: srvc),
)
];
收件人:
List<SingleChildWidget> dependentServices = [
ListenableProxyProvider<EventService, EventOverviewModel>(
update: (_, srvc, __) => EventOverviewModel(eventService: srvc),
)
];
我猜想EventService或EventOverviewModel扩展或混合了ChangeNotifier
,并且您正在代码中的某个地方监听来更改Provider<EventService>
和ProxyProvider<EventOverviewModel>
的值,但是除非您的提供商“可以收听”,否则这些更改将永远不会被广播/接收。
因此,如果您的模型扩展/混合了ChangeNotifier
,并且他们正在调用notifyListeners()
,则这些模型的提供者必须至少为“可监听”。
ListenableProvider
ListenableProxyProvider
...或者更好的是,您的模型可以使用ChangeNotifier版本:
ChangeNotifierProvider
ChangeNotifierProxyProvider
...当这些对象不再使用时,可以方便地放置/分离所有侦听器(释放资源)。