Created
February 12, 2019 16:37
-
-
Save ruan65/41d1aa37402e49eea065c5bbe0698853 to your computer and use it in GitHub Desktop.
Bloc Flutter Example
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
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