-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.dart
194 lines (147 loc) · 5.17 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'package:rx_bloc/rx_bloc.dart';
import 'package:rxdart/rxdart.dart';
void main() async {
print('----------RX_BLOC----------');
/// Create a `CounterBloc` instance.
final bloc = CounterBloc(ServerSimulator());
/// Listen to the `count` state.
bloc.states.count.listen((int number) {
print('onChange -- Count state changed to $number');
});
/// Listen to the `error` state.
bloc.states.errors.listen((String error) {
print('onError -- $error');
});
/// Listen to the `loading` state.
bloc.states.isLoading.listen((bool isLoading) {
print(isLoading ? 'Loading...' : 'Loaded ✔ \n');
});
await Future.delayed(const Duration(milliseconds: 500));
/// Fire the increment event.
bloc.events.increment(); // 1
await Future.delayed(const Duration(milliseconds: 500));
/// Decrementing.
bloc.events.decrement(); // 0
/// Decrementing one more time will cause an error.
await Future.delayed(const Duration(milliseconds: 500));
bloc.events.decrement(); // Exception.
}
/// This BloC and its event and state contracts usually
/// resides in `counter_bloc.dart`
/// A contract class containing all events.
abstract class CounterBlocEvents {
/// Increment the count event.
void increment();
/// Decrement the count event.
void decrement();
}
/// A contract class containing all states for our multi state BloC.
abstract class CounterBlocStates {
/// The count of the Counter
///
/// It can be controlled by executing [CounterBlocEvents.increment] and
/// [CounterBlocEvents.decrement]
///
Stream<int> get count;
/// Loading state
Stream<bool> get isLoading;
/// Error messages
Stream<String> get errors;
/// ... state n
/// You can have as many states as you need.
/// You're not limited to 1 state for a bloc class.
}
/// A RX `CounterBloc` which maps multiple events with multiple states.
class CounterBloc extends $CounterBloc {
CounterBloc(this._server);
final ServerSimulator _server;
/// Map increment and decrement events to `count` state.
@override
Stream<int> _mapToCountState() => Rx.merge<Result<int>>([
// On increment.
_$incrementEvent.flatMap((_) => _server.increment().asResultStream()),
// On decrement.
_$decrementEvent.flatMap((_) => _server.decrement().asResultStream()),
])
// This automatically handles the error and loading state.
.setResultStateHandler(this)
// Provide success response only.
.whereSuccess()
//emit 0 as initial value
.startWith(0);
/// Transform any exception into a readable string.
@override
Stream<String> _mapToErrorsState() =>
errorState.map((result) => result.toString());
@override
Stream<bool> _mapToIsLoadingState() => loadingState;
}
/// This will simulate a server with 100 milliseconds response time.
class ServerSimulator {
int _counter = 0;
static const delay = Duration(milliseconds: 100);
static const delayTest = Duration(milliseconds: 110);
Future<int> increment() async {
// Server response time.
await Future.delayed(delay);
if (_counter >= 3) {
throw Exception('Maximum number is reached!');
}
return ++_counter;
}
Future<int> decrement() async {
// Server response time.
await Future.delayed(delay);
// Simulate an error from the server when the counter goes less than 1.
if (_counter <= 0) {
throw Exception('Minimum number is reached!');
}
return --_counter;
}
}
/// NO NEED TO ANALYZE IT!
///
/// The code below will be automatically generated
/// for you by `rx_bloc_generator`.
///
/// This generated class usually resides in [file-name].rxb.g.dart.
/// Find more info at https://pub.dev/packages/rx_bloc_generator.
/// ********************GENERATED CODE**************************************
/// CounterBlocType class used for bloc event and state access from widgets
abstract class CounterBlocType extends RxBlocTypeBase {
CounterBlocEvents get events;
CounterBlocStates get states;
}
/// $CounterBloc class - extended by the CounterBloc bloc
abstract class $CounterBloc extends RxBlocBase
implements CounterBlocEvents, CounterBlocStates, CounterBlocType {
final _$decrementEvent = PublishSubject<void>();
@override
void decrement() => _$decrementEvent.add(null);
final _$incrementEvent = PublishSubject<void>();
@override
void increment() => _$incrementEvent.add(null);
late final Stream<int> _countState = _mapToCountState();
@override
Stream<int> get count => _countState;
Stream<int> _mapToCountState();
late final Stream<bool> _isLoadingState = _mapToIsLoadingState();
@override
Stream<bool> get isLoading => _isLoadingState;
Stream<bool> _mapToIsLoadingState();
late final Stream<String> _errorsState = _mapToErrorsState();
@override
Stream<String> get errors => _errorsState;
Stream<String> _mapToErrorsState();
@override
CounterBlocEvents get events => this;
@override
CounterBlocStates get states => this;
/// Dispose of all the opened streams when the bloc is closed.
@override
void dispose() {
_$incrementEvent.close();
super.dispose();
}
}
/// ********************GENERATED CODE END**************************************