Created
July 22, 2025 14:59
-
-
Save dickermoshe/e9138e210c121f6315310f6cd4d7085a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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