Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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);
});
like image 359
Lara Avatar asked Aug 31 '25 16:08

Lara


1 Answers

This error is happening because you tried to listen to a provider marked with .autoDispose in a provider that is not marked with .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.

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/

like image 178
Eric Valero Avatar answered Sep 05 '25 15:09

Eric Valero