Riverpod Simple State Provider

edited December 2022 in Flutter


Packages Used:


flutter_riverpod: ^2.1.1

1.Declaring simple counter provider

final counterProvider = StateProvider((ref) => 0);

2. To change the value of counter provider

ref.read(counterProvider.notifier).state++;

3. To observer the changes

final int counterResult = ref.watch(counterProvider);

4.To reset the provider(values to initial state) we can use

        a.ref.invalidate(counterProvider); 
                   or
               b.ref.refresh(counterProvider);

5.To reset the state of the provider when we move from one screen to other we can use autodispose while declaring provider like

final counterProvider = StateProvider.autoDispose((ref) => 0);

6.if you want to dosomethig after getting data we can listen to the provider by using below

ref.listen(counterProvider, (previous, next) {});


Sign In or Register to comment.