Last active
March 31, 2022 05:25
-
-
Save macoshita/de272d1b93e6b0dc5a35ffa89f28f206 to your computer and use it in GitHub Desktop.
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 'package:riverpod/riverpod.dart'; | |
final counter = StateProvider((_) => 0); | |
final counter2 = StateProvider((ref) => ref.watch(counter)); | |
Future<void> main() async { | |
final container = ProviderContainer(); | |
// counter2 を listen | |
container.read(counter2.notifier).stream.listen( | |
(i) => print('come $i'), | |
onDone: () => print('done'), | |
); | |
// counter を 1, 2 と変化させたら、 | |
// 上記 listen により come 1, come 2 と表示されることを期待するがそうならず、 | |
// 以下のように表示される | |
// | |
// send 1 | |
// done | |
// send 2 | |
print('send 1'); | |
container.read(counter.notifier).state = 1; | |
await Future.delayed(const Duration(seconds: 1)); | |
print('send 2'); | |
container.read(counter.notifier).state = 2; | |
await Future.delayed(const Duration(seconds: 1)); | |
container.dispose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment