Skip to content

Instantly share code, notes, and snippets.

@macoshita
Last active March 31, 2022 05:25
Show Gist options
  • Save macoshita/de272d1b93e6b0dc5a35ffa89f28f206 to your computer and use it in GitHub Desktop.
Save macoshita/de272d1b93e6b0dc5a35ffa89f28f206 to your computer and use it in GitHub Desktop.
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