Ngăn chặn đã thử sử dụng Nhà cung cấp với một kiểu con của lỗi rung / Nghe được


9

Tôi muốn sử dụng gói nhà cung cấp (4.0) trong dự án rung của mình nhưng tôi nhận được cảnh báo / lỗi "Đã thử sử dụng Nhà cung cấp với một kiểu con của lỗi / cảnh báo Nghe / Phát trực tuyến".

Chòm sao của tôi:

Tôi có một Api()lớp có chức năng CRUD cơ sở cho lưu trữ đám mây.

Tôi có một EventService()lớp sử dụng Api()lớp với tham số cụ thể.

Đối với quan điểm của tôi, tôi có một lớp ViewModel được gọi là EventOverviewModelsử dụng EventService.

Cấu hình nhà cung cấp của tôi trông như thế này:

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 = [];

Mọi thứ dường như hoạt động trong ứng dụng nhưng tôi vẫn nhận được "Đã thử sử dụng Nhà cung cấp với một kiểu con của Nghe / Luồng".

Làm thế nào tôi nên thay đổi mã của tôi để không nhận được thông báo lỗi này nữa?

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 ...

Câu trả lời:


6

Hãy thử thay đổi

List<SingleChildWidget> independentServices = [
  Provider<EventService>(create: (_) => EventService(api: Api('events')))
];

đến:

List<SingleChildWidget> independentServices = [
  ListenableProvider<EventService>(create: (_) => EventService(api: Api('events')))
];

List<SingleChildWidget> dependentServices = [
  ProxyProvider<EventService, EventOverviewModel>(
    update: (_, srvc, __) => EventOverviewModel(eventService: srvc),
  )
];

đến:

List<SingleChildWidget> dependentServices = [
  ListenableProxyProvider<EventService, EventOverviewModel>(
    update: (_, srvc, __) => EventOverviewModel(eventService: srvc),
  )
];

Tôi đoán EventService hoặc EventOverviewModel mở rộng hoặc mixin ChangeNotifiervà ở đâu đó trong mã của bạn, bạn đang lắng nghe những thay đổi về giá trị Provider<EventService>ProxyProvider<EventOverviewModel>, nhưng những thay đổi đó sẽ không bao giờ được phát / nhận trừ khi nhà cung cấp của bạn "Nghe được".

Vì vậy, nếu các mô hình của bạn mở rộng / mixin ChangeNotifiervà chúng đang gọi notifyListeners(), các nhà cung cấp của các mô hình đó cần phải "Nghe được".

ListenableProvider
ListenableProxyProvider

cảm ơn bro cũng lưu ý rằng tài liệu khuyên bạn nên sử dụng ChangeNotifierProxyProvider thay vì ListenableProxyProvider và cả hai đều cần tạo tham số trả về trạng thái ban đầu của EventOverviewModel
Ahmed D. Sherif
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.