简体   繁体   English

Flutter Riverpod -.autoDispose - 参数类型“AutoDisposeProvider”不能分配给参数类型“AlwaysAliveProviderBase”

[英]Flutter Riverpod - .autoDispose - The argument type 'AutoDisposeProvider' can't be assigned to the parameter type 'AlwaysAliveProviderBase'

According to the docs when I'm getting this error I am supposed to mark both Providers with .autoDispose :根据文档,当我收到此错误时,我应该用.autoDispose标记两个Providers

The argument type 'AutoDisposeProvider' can't be assigned to the parameter type 'AlwaysAliveProviderBase'参数类型“AutoDisposeProvider”不能分配给参数类型“AlwaysAliveProviderBase”

Why am I still getting the error in this minimalistic example?为什么在这个简约示例中我仍然收到错误?

final a = FutureProvider.autoDispose<List<String>>((ref) {
  return Future.value(["test"]);
});

final b = FutureProvider.autoDispose<List<String>>((ref) {
  return ref.watch(a);
});

It's not because of the autoDispose .这不是因为autoDispose If you replace the code with the following code, you'll get an error again:如果您将代码替换为以下代码,您将再次收到错误:

// Removed "autoDispose"
final a = FutureProvider<List<String>>((ref) {
  return Future.value(["test"]);
});

final b = FutureProvider<List<String>>((ref) {
  return ref.watch(a);
});

The Error:错误:

The argument type 'FutureProvider<List<String>>' can't be assigned to the parameter type 'AlwaysAliveProviderListenable<FutureOr<List<String>>>'.

The reason is that value of the a provider is an AsyncValue so b provider should returns a AsyncValue<List<String>> instead of List<String> if it return the data directly.原因是a提供者的值是AsyncValue ,因此如果b提供者直接返回数据,则应该返回AsyncValue<List<String>>而不是List<String>

final a = FutureProvider.autoDispose<List<String>>((ref) {
  return Future.value(["test"]);
});

final b = FutureProvider.autoDispose<AsyncValue<List<String>>>((ref) {
 return ref.watch(a);
});

Or it can use the value and process it and then returns another list based on that, something like this:或者它可以使用该值并对其进行处理,然后基于该值返回另一个列表,如下所示:

final a = FutureProvider.autoDispose<List<String>>((ref) {
  return Future.value(["test"]);
});

final b = FutureProvider.autoDispose<List<String>>((ref) async {
  final value = ref.watch(a);

  // Doing another operation
  await Future.delayed(const Duration(seconds: 2));

  return value.maybeWhen(
    data: (data) => data.map((e) => 'B $e').toList(),
    orElse: () => [],
  );
});

This error is happening because you tried to listen to a provider marked with.autoDispose in a provider that is not marked with.autoDispose.发生此错误是因为您尝试在未标记为 .autoDispose 的提供程序中侦听标记为 .autoDispose 的提供程序。

final firstProvider = Provider.autoDispose((ref) => 0);

final secondProvider = Provider((ref) {
  // The argument type 'AutoDisposeProvider<int>' can't be assigned to the
  // parameter type 'AlwaysAliveProviderBase<Object, Null>'
  ref.watch(firstProvider);
});

The solution is marking the secondProvider with.autoDispose.解决方案是使用 .autoDispose 标记 secondProvider。

final firstProvider = Provider.autoDispose((ref) => 0);

final secondProvider = Provider.autoDispose((ref) {
  ref.watch(firstProvider);
});

I just copy the solution from the official Riverpod documentation at the bottom of the page: https://riverpod.dev/docs/concepts/modifiers/auto_dispose/我只是从页面底部的官方 Riverpod 文档中复制解决方案: https://riverpod.dev/docs/concepts/modifiers/auto_dispose/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Argument 类型 &#39;Context&#39; 不能分配给参数类型 &#39;BuildContext&#39; - Flutter - The Argument type 'Context' can't be assigned to the parameter type 'BuildContext' - Flutter Flutter - 参数类型 &#39;Complexity/*1*/&#39; 不能分配给参数类型 &#39;Complexity/*2*/ - Flutter - argument type 'Complexity/*1*/' can't be assigned to the parameter type 'Complexity/*2*/ 参数类型“对象?” 不能分配给参数类型“字符串”。 颤动 - The argument type 'Object?' can't be assigned to the parameter type 'String'. with flutter Flutter 错误,参数类型 '' 不能分配给参数类型 - Flutter error, The argument type '' can't be assigned to the parameter type Flutter listener error The argument Type can't be assigned to the parameter type - Flutter listener error The argument Type can't be assigned to the parameter type 参数类型“String”不能分配给参数类型“Uri”。 Flutter - The argument type 'String' can't be assigned to the parameter type 'Uri'. Flutter 参数类型“JsObject”不能分配给参数类型“BuildContext”。 - flutter - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'. - flutter 参数类型 SearchBar 不能分配给 flutter 中的参数类型 Widget - The argument type SearchBar can't be assigned to the parameter type Widget in flutter 不能将参数类型“String”分配给参数类型“TextEditingController?” 在 Flutter - The argument type 'String' can't be assigned to the parameter type 'TextEditingController?' in Flutter 参数类型“区域?” 无法分配给 flutter 中的参数类型“区域” - the argument type 'Zone?' can't be assigned to the parameter type 'Zone' in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM