Okan
Okan

Reputation: 41

Best practice for passing param Riverpod's providers only once

I just want to build a Provider which asks params only one and inits correctly.

Here my tryouts:

Here is my simple code:

class MyHomePage extends ConsumerWidget {
  const MyHomePage({super.key});

  final myString = 'Hey';

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    //Not worked
    ProviderContainer(
        overrides: [messageProvider.overrideWith(() => ViewModel(myString))]);

    return Scaffold(
      body: ProviderScope(
        //Not worked either
        overrides: [messageProvider.overrideWith(() => ViewModel(myString))],
        child: Center(
          //I just didn't use .when to shorter code
          child: Text(ref.watch(messageProvider).value!.counter.toString()),
        ),
      ),
    );

  }
}
final messageProvider = AsyncNotifierProvider.autoDispose<ViewModel, Model>(
    () => throw UnimplementedError());

class ViewModel extends AutoDisposeAsyncNotifier<Model> {
  final String param;

  ViewModel(this.param);

  @override
  FutureOr<Model> build() {
    //Make some fetch with param, (only once!)
    return Model(param.length);
  }
}

When I run that. It gives UnimplementedError

Waiting your suggestions & fixes. Thanks in advance!

Expected: Works properly.

Upvotes: 1

Views: 816

Answers (1)

Madushan
Madushan

Reputation: 7468

@riverpod
ViewModel myViewModel(MyViewModelRef ref, String param){
    return ViewModel(param);
}

This is autoDispose by default in Riverpod 2. If you don't want to auto dispose you can use @Riverpod(keepalive:true) instead of @riverpod

If you don't want to pass the param to the provider, you can eliminate it and hardcode the value to the ViewModel, but at that point, if there are no other dependencies, might as well make it a public final variable in some file, since it looks like this is a singleton that never changes so it is questionable what you'd achieve by making it a Riverpod provider.

Upvotes: 0

Related Questions