Skip to content

Instantly share code, notes, and snippets.

@ruan65
Created February 12, 2019 16:37
Show Gist options
  • Save ruan65/41d1aa37402e49eea065c5bbe0698853 to your computer and use it in GitHub Desktop.
Save ruan65/41d1aa37402e49eea065c5bbe0698853 to your computer and use it in GitHub Desktop.
Bloc Flutter Example
class CounterState {
final int counter;
CounterState._(this.counter);
factory CounterState.nextState(int times) => CounterState._(times);
}
class CounterBloc extends Bloc<CounterEvent, CounterState> {
@override
CounterState get initialState => CounterState.nextState(0);
@override
Stream<CounterState> mapEventToState(
CounterState currentState,
CounterEvent event,
) async* {
if(event is IncrementEvent) {
yield CounterState.nextState(currentState.counter + 1);
} else if(event is DecrementEvent) {
yield CounterState.nextState(currentState.counter - 1);
}
}
void onIncrement() {
dispatch(IncrementEvent());
}
void onDecrement() {
dispatch(DecrementEvent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment