Skip to content

Instantly share code, notes, and snippets.

@FantasyCheese
Last active September 18, 2020 15:21
Show Gist options
  • Save FantasyCheese/384384b8f907fa84a400bf0cccf961bb to your computer and use it in GitHub Desktop.
Save FantasyCheese/384384b8f907fa84a400bf0cccf961bb to your computer and use it in GitHub Desktop.
Simplest Riverpod without Flutter Example
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