Skip to content

Instantly share code, notes, and snippets.

@dickermoshe
Created July 22, 2025 14:59
Show Gist options
  • Save dickermoshe/e9138e210c121f6315310f6cd4d7085a to your computer and use it in GitHub Desktop.
Save dickermoshe/e9138e210c121f6315310f6cd4d7085a to your computer and use it in GitHub Desktop.
import "package:riverpod/riverpod.dart";
void main() {
// A `ProviderContainer` is the central storage for the state of all providers.
final container = ProviderContainer();
// A `StateProvider` is used to expose a simple, mutable piece of state.
// In this case, 'counter' holds an integer value, initialized to 0.
final counter = StateProvider((ref) => 0);
// `container.listen` allows you to observe changes in a provider's state.
container.listen(counter, (prevCount, currentCount) {
print(currentCount);
});
// Setting `state = 1` will trigger the `container.listen` callback.
// Logs: 1
container.read(counter.notifier).state = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment