-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdart_event_notifier_example.dart
57 lines (46 loc) · 1.54 KB
/
dart_event_notifier_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'package:dart_event_notifier/dart_event_notifier.dart';
sealed class CounterEvent {
const CounterEvent();
}
final class Incremented extends CounterEvent {
const Incremented();
}
final class Decremented extends CounterEvent {
const Decremented();
}
class Counter with EventNotifier<CounterEvent> {
var _value = 0;
int get value => _value;
void increment() {
_value++;
notify(Incremented());
}
void decrement() {
_value--;
notify(Decremented());
}
}
Future<void> main() async {
final counter = Counter();
counter.events.listen((event) {
print('Received event: $event; current value: ${counter.value}');
});
// This will print twice: once with the Incremented event, and once with
// the Decremented event. In both cases the value is 0 because the stream
// callback is asynchronous while the counter has been incremented
// and decremented synchronously.
counter
..increment()
..decrement();
// -> Received event: Instance of 'Incremented'; current value: 0
// -> Received event: Instance of 'Decremented'; current value: 0
await Future<void>.delayed(Duration.zero);
// This will print twice as well with the same events but the values are now
// 1 and 0, respectively. This is because the print callback had a chance
// to run before `decrement` was called.
counter.increment();
// -> Received event: Instance of 'Incremented'; current value: 1
await Future<void>.delayed(Duration.zero);
counter.decrement();
// -> Received event: Instance of 'Decremented'; current value: 0
}