Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Last active January 15, 2022 12:18
Show Gist options
  • Save rrousselGit/28da2142bb08a886d1529a52af38ada9 to your computer and use it in GitHub Desktop.
Save rrousselGit/28da2142bb08a886d1529a52af38ada9 to your computer and use it in GitHub Desktop.
raw Riverpod
import 'package:riverpod/riverpod.dart';
void main() {
final container = ProviderContainer();
container.listen<Counter>(counter, (prev, value) {
print('count: ${value.count}');
}, fireImmediately: true);
container.read(counter).increment();
}
class Counter {
Counter({required this.count, required this.increment});
final int count;
final void Function() increment;
Counter copyWith({int? count}) {
return Counter(count: count ?? this.count, increment: increment);
}
}
final counter = Provider<Counter>((ref) {
return Counter(
count: 0,
increment: () {
ref.state = ref.state.copyWith(count: ref.state.count + 1);
},
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment