I'm building a flutter app that uses BLoC pattern and using hydrated_bloc package to persist data.
The bloc itself is working fine, ie the event comes into the bloc and the bloc yields a state back to the ui.
The issue is that the bloc is not saving the json with the hydrated_bloc.
Here's the code for the bloc and event:
enum ConditionsEvent { snowyPressed, sunnyPressed, rainyPressed }
class ConditionsBloc extends HydratedBloc<ConditionsEvent, String> {
ConditionsBloc() : super('unknown');
@override
Stream<String> mapEventToState(ConditionsEvent event) async* {
switch (event) {
case ConditionsEvent.snowyPressed:
yield 'Snowy';
break;
case ConditionsEvent.sunnyPressed:
yield 'Sunny';
break;
case ConditionsEvent.rainyPressed:
yield 'Rainy';
break;
}
throw UnimplementedError();
}
@override
Map<String, dynamic> toJson(String state) {
return <String, String>{'conditions': state};
}
@override
String fromJson(Map<String, dynamic> json) => json['value'] as String;
}
How can I get this to save the data in local storage using hydrated_bloc, so that when the user restarts app, the data is persisted? I think the problem is with toJson
and fromJson
.