Last active
January 15, 2022 12:18
-
-
Save rrousselGit/28da2142bb08a886d1529a52af38ada9 to your computer and use it in GitHub Desktop.
raw Riverpod
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() { | |
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