Search code examples
flutterriverpodriverpod-generator

How to prevent provider from auto disposing when using riverpod_generator


I am using riverpod_generator and I don't see any way of keeping the provider alive. The generator code is actually equivalent to

final fooProvider = FutureProvider.autoDispose((ref) async => 0);

But in above code I could remove autoDispose but how can I do that with @riverpod generator?

@riverpod
Future<int> foo(FooRef ref) async {
  return 0; // Where to define `autoDispose`? 
}

Solution

  • All providers auto dispose when you generate them using the generator. But you can set keepAlive to change this behavior.

    @Riverpod(keepAlive: true)
    Future<int> foo(FooRef ref) async {
      return 0;
    }