Skip to content

Instantly share code, notes, and snippets.

@felangel
Created February 14, 2020 17:14
Show Gist options
  • Save felangel/ce3ed5b80bb2445e16f4bb7115205d1c to your computer and use it in GitHub Desktop.
Save felangel/ce3ed5b80bb2445e16f4bb7115205d1c to your computer and use it in GitHub Desktop.
bloc + combineLatest
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