Created
June 18, 2019 05:36
-
-
Save felangel/bcc6ad8674029a5acf7b5625b3bd4ca0 to your computer and use it in GitHub Desktop.
[flutter_bloc_hydrated_weather] SettingsBloc
This file contains 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 SettingsBloc extends HydratedBloc<SettingsEvent, SettingsState> { | |
@override | |
SettingsState get initialState => | |
super.initialState ?? | |
SettingsState(temperatureUnits: TemperatureUnits.celsius); | |
@override | |
Stream<SettingsState> mapEventToState(SettingsEvent event) async* { | |
if (event is TemperatureUnitsToggled) { | |
yield SettingsState( | |
temperatureUnits: | |
currentState.temperatureUnits == TemperatureUnits.celsius | |
? TemperatureUnits.fahrenheit | |
: TemperatureUnits.celsius, | |
); | |
} | |
} | |
@override | |
SettingsState fromJson(Map<String, dynamic> json) { | |
return SettingsState( | |
temperatureUnits: | |
TemperatureUnits.values[json['temperatureUnits'] as int], | |
); | |
} | |
@override | |
Map<String, dynamic> toJson(SettingsState state) { | |
return {'temperatureUnits': state.temperatureUnits.index}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment