Last active
September 18, 2020 15:21
-
-
Save FantasyCheese/384384b8f907fa84a400bf0cccf961bb to your computer and use it in GitHub Desktop.
Simplest Riverpod without Flutter Example
This file contains 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 'dart:async'; | |
import 'package:flutter_riverpod/all.dart'; | |
final container = ProviderContainer(); // declare global ProviderContainer | |
final countProvider = StateProvider((ref) => 0); // declare providers anywhere | |
void main() { | |
final ProviderSubscription<StateController<int>> subscription = container.listen(countProvider, didChange: (sub) { // listen from container | |
print(sub.read().state); | |
}); | |
container.read(countProvider).stream.listen((event) { // or listen from stream | |
print(event); | |
}); | |
Timer.periodic(Duration(seconds: 1), (timer) { | |
container.read(countProvider).state++; // update state | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment