I am trying to use Hydrated Bloc to persist the state in my app. The tutorials I have found are all using previous versions of Hydrated Blocs and use the BlocSupervisor, which was removed in the Dart version of the bloc package in v.5 (https://pub.dev/packages/bloc/changelog). flutter_bloc and hydrated_bloc removed it when they updated to bloc v.5 (https://pub.dev/packages/flutter_bloc/changelog and https://pub.dev/packages/hydrated_bloc/changelog).
The documentation says it should be replaced with the BlocObserver. hydrated_bloc and flutter_bloc do not list a replacement. So far, I have not found a HydratedBloc tutorial that uses anything other than BlocSupervisor and BlocDelegate; only flutter_bloc tutorials.
How do I create the HydratedBloc equivalent of the BlocObserver in order to persist state?
EDIT: Okay, I think I'm on the right track now thanks to your example.
Here is the relevant portion of my code, with the classes renamed:
class ClassABLoC
extends HydratedBloc<ClassAEvent, ClassAState> {
//This does initialize it with some values, but not the ones from storage. Trying to call fromJson() there gives an error.
ClassABLoC() : super(ClassAState.dataNotReceived());
@override
Stream<classAState> mapEventToState(
classAEvent event) async* {
if (event is classAInitialize){
print("Initializing");
yield fromJson(json.decode(
HydratedBloc.storage.read("vehicleNumber") as String,
) as Map<String, dynamic>,
);
}
if (event is classAValidate) {
yield classAState.validated(
theEvent: event,
numberValidated: validateTheInput(event.numberVal, event.vehicleNumber),
distanceValidated:
validateTheInput(event.distanceVal, event.vehicleDistanceTraveled),
yearValidated: validateTheInput(event.yearVal, event.vehicleYear),
vinValidated: validateTheInput(event.vinVal, event.vin),
licensePlateValidated:
validateTheInput(event.licensePlateVal, event.vehicleLicensePlate),
revsPerDistValidated:
validateTheInput(event.revsPerDistVal, event.vehicleRevsPerDist),
fuelTypeValidated: validateTheInput(event.fuelTypeVal, event.fuelType),
fuelCapacityValidated:
validateTheInput(event.fuelCapacityVal, event.fuelCapacity),
siteValidated: validateTheInput(event.siteVal, event.vehicleSite),
numberError: event.numberVal!.validationFailedMsg,
distanceError: event.distanceVal!.validationFailedMsg,
yearError: event.yearVal!.validationFailedMsg,
vinError: event.vinVal!.validationFailedMsg,
licensePlateError: event.licensePlateVal!.validationFailedMsg,
revsPerDistError: event.revsPerDistVal!.validationFailedMsg,
fuelTypeError: event.fuelTypeVal!.validationFailedMsg,
fuelCapacityError: event.fuelCapacityVal!.validationFailedMsg,
siteError: event.siteVal!.validationFailedMsg,
);
} else if (event is StoreDataEvent) {
toJson(StoreDataState(
vehicleNumber: event.vehicleNumber,
vehicleYear: event.vehicleYear,
vehicleRevsPerDist: event.vehicleRevsPerDist,
vehicleDistanceTraveled: event.vehicleDistanceTraveled,
vin: event.vin,
vehicleLicensePlate: event.vehicleLicensePlate,
fuelCapacity: event.fuelCapacity,
fuelType: event.fuelType,
vehicleSite: event.vehicleSite,
));
}
}
Map<String, dynamic>? toJson(ProgramDataTracSVTState state) {
if (state is StoreDataState) {
print("State was StoreDataState.");
print(state.toString());
return {
'vehicleNumber': state.vehicleNumber,
'vehicleDistanceTraveled': state.vehicleDistanceTraveled,
'vehicleLicensePlate': state.vehicleLicensePlate,
'vin': state.vin,
'rehicleRevsPerDist': state.vehicleRevsPerDist,
'vehicleSite': state.vehicleSite,
'vehicleYear': state.vehicleYear,
'fuelCapacity': state.fuelCapacity,
'fuelType': state.fuelType,
'distUnit': state.distUnits,
};
}
}
ProgramDataTracSVTState fromJson(Map<String, dynamic> json) {
print(json['vehicleDistance'] as String);
return ProgramDataTracSVTState(
vehicleNumber: (json['vehicleNumber'] as String?) == "" ||
json['vehicleNumber'] == null
? "HARDCODED"
: json['vehicleNumber'] as String,
vehicleDistanceTraveled:
(json['vehicleDistanceTraveled'] as String?) == "" ||
(json['vehicleDistanceTraveled'] as String?) == null
? "HARDCODED VALUE"
: json['vehicleDistanceTraveled'] as String,
vehicleLicensePlate: (json['vehicleLicensePlate'] as String?) == "" ||
(json['vehicleLicensePlate'] as String?) == null
? ""
: json['vehicleLicensePlate'] as String,
vin: (json['vin'] as String?) == "" || (json['vin'] as String?) == null
? "HARDCODED VALUE"
: json['vin'] as String,
vehicleRevsPerDist: (json['vehicleRevsPerDist'] as String?) == "" ||
(json['vehicleRevsPerDist'] as String?) == null
? "500"
: json['vehicleRevsPerDist'] as String,
vehicleSite: (json['vehicleSite'] as String?) == "" ||
(json['vehicleSite'] as String?) == null
? "Home Base Site"
: (json['vehicleSite'] as String),
vehicleYear: (json['vehicleYear'] as String?) == "" ||
(json['vehicleYear'] as String?) == null
? "2018"
: json['vehicleYear'] as String,
fuelCapacity: (json['fuelCapacity'] as String?) == "" ||
(json['fuelCapacity'] as String?) == null
? ""
: (json['fuelCapacity'] as String),
fuelType: (json['fuelType'] as String?) == "" ||
(json['fuelType'] as String?) == null
? "Diesel"
: (json['fuelType'] as String),
distUnits: (json['distUnit'] as String?) == "" ||
(json['distUnit'] as String?) == null
? "None"
: (json['distUnit'] as String),
);
}
I need to get more than one string's worth of data from the storage. How would I do that? I can't combine the JSON strings as far as I know. Thank you so much for your help!
