Created
February 14, 2020 17:14
-
-
Save felangel/ce3ed5b80bb2445e16f4bb7115205d1c to your computer and use it in GitHub Desktop.
bloc + combineLatest
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 'dart:async'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:rxdart/rxdart.dart'; | |
enum CounterEvent { increment } | |
class CounterBloc extends Bloc<CounterEvent, int> { | |
@override | |
int get initialState => 0; | |
@override | |
Stream<int> mapEventToState( | |
CounterEvent event, | |
) async* { | |
switch (event) { | |
case CounterEvent.increment: | |
yield state + 1; | |
break; | |
} | |
} | |
} | |
void main() async { | |
final counterBlocA = CounterBloc(); | |
final counterBlocB = CounterBloc(); | |
Rx.combineLatest2(counterBlocA, counterBlocB, ((a, b) { | |
return [a, b]; | |
})).listen((combinedStates) { | |
final stateA = combinedStates.first; | |
final stateB = combinedStates.last; | |
print('($stateA, $stateB)'); | |
}); | |
Timer.periodic( | |
Duration(seconds: 1), | |
(_) => counterBlocA.add(CounterEvent.increment), | |
); | |
Timer.periodic( | |
Duration(seconds: 2), | |
(_) => counterBlocB.add(CounterEvent.increment), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment