Home > Enterprise >  Flutter Riverpod - .autoDispose - The argument type 'AutoDisposeProvider' can't be as
Flutter Riverpod - .autoDispose - The argument type 'AutoDisposeProvider' can't be as

Time:07-16

According to the docs when I'm getting this error I am supposed to mark both Providers with .autoDispose:

The argument type 'AutoDisposeProvider' can't be assigned to the parameter type '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);
});

CodePudding user response:

It's not because of the 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.

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: () => [],
  );
});
  • Related