【发布时间】:2021-12-05 17:09:49
【问题描述】:
我正在尝试使用 Hydrated Bloc 在我的应用程序中保持状态。我发现的教程都是使用以前版本的 Hydrated Blocs 并使用 BlocSupervisor,它在 v.5 (https://pub.dev/packages/bloc/changelog) 的 bloc 包的 Dart 版本中被删除。 flutter_bloc 和 hydrated_bloc 在更新到 bloc v.5(https://pub.dev/packages/flutter_bloc/changelog 和 https://pub.dev/packages/hydrated_bloc/changelog)时将其删除。
文档说它应该替换为BlocObserver。 hydrated_bloc 和 flutter_bloc 没有列出替代品。到目前为止,我还没有找到使用除BlocSupervisor 和BlocDelegate 之外的任何东西的HydratedBloc 教程;仅限flutter_bloc 教程。
如何创建与 BlocObserver 等效的 HydratedBloc 以保持状态?
编辑: 好的,感谢您的示例,我认为我现在走在了正确的轨道上。
这是我的代码的相关部分,类已重命名:
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),
);
}
我需要从存储中获取多个字符串的数据。我该怎么做?据我所知,我无法组合 JSON 字符串。 非常感谢您的帮助!
【问题讨论】:
-
pub.dev/packages/hydrated_bloc/example 此处提供的示例是如何保持状态的工作示例。您要保留哪种类型的数据?
-
@Loren.A,谢谢!我正在尝试保留文本字符串。 toJson 方法设置正确,并且会在热重载之间保持状态,但不会在重启之间保持状态。
-
您应该覆盖
toJson和fromJson。你能分享那些吗?一旦您扩展HydratedBloc,它应该会提示您。 -
抱歉,当我看到这个时,已经在周末无法访问代码!添加了 toJson 和 fromJson。