gpt4 book ai didi

flutter - 卡住如何在顶级模型上分配我自己的 JsonConverter?

转载 作者:行者123 更新时间:2023-12-05 01:56:48 26 4
gpt4 key购买 nike

我已经卡住模型(简化):

part 'initial_data_model.freezed.dart';
part 'initial_data_model.g.dart';

@freezed
class InitialDataModel with _$InitialDataModel {
const factory InitialDataModel() = Data;

const factory InitialDataModel.loading() = Loading;

const factory InitialDataModel.error([String? message]) = Error;

factory InitialDataModel.fromJson(Map<String, dynamic> json) => _$InitialDataModelFromJson(json);
}

文档说明了如何在字段而不是模型本身上分配自定义转换器

我从后端和 api_provider 的某个地方得到了 json
返回 InitialDataModel.fromJson(json);
我无法控制 json 结构,没有“runtimeType”和其他愚蠢的冗余东西

当我想从 json 创建模型时,我调用 fromJson 我有这个

flutter: CheckedFromJsonException
Could not create `InitialDataModel`.
There is a problem with "runtimeType".
Invalid union type "null"!

好的,再来一次
我有 api_provider

final apiProvider = Provider<_ApiProvider>((ref) => _ApiProvider(ref.read));

class _ApiProvider {
final Reader read;

_ApiProvider(this.read);

Future<InitialDataModel> fetchInitialData() async {
final result = await read(repositoryProvider).send('/initial_data');
return result.when(
(json) => InitialDataModel.fromJson(json),
error: (e) => InitialDataModel.error(e),
);
}
}

你可能会看到我正在尝试从 json 创建 InitialDataModel

这一行抛出我上面提到的错误

我不明白如何从 json 创建 InitialDataModel,现在在我的示例中它只是空模型,没有字段

(json) => InitialDataModel.fromJson(json),
json 这里是 Map,即使我传递简单的空 map {} 而不是真正的 json 对象,它也会显示错误

最佳答案

最简单的解决方案是使用正确的构造函数而不是 _$InitialDataModelFromJson。示例:

@freezed
class InitialDataModel with _$InitialDataModel {
const factory InitialDataModel() = Data;

...

factory InitialDataModel.fromJson(Map<String, dynamic> json) => Data.fromJson(json);
}

缺点当然是只有在确定 json 正确时才能使用 fromJson,这不是很好。我实际上不推荐这种方式,因为它让调用者承担检查有效性和调用正确构造函数的负担。


另一个可能是最好的解决方案是遵循 documentation并创建一个自定义转换器,即使这需要您有两个单独的类。


否则,您可以选择不同的方法并将数据类与联合分开,这样您将拥有一个仅用于请求状态的联合和一个用于成功响应的数据类:

@freezed
class InitialDataModel with _$InitialDataModel {
factory InitialDataModel(/* here go your attributes */) = Data;

factory InitialDataModel.fromJson(Map<String, dynamic> json) => _$InitialDataModelFromJson(json);
}

@freezed
class Status with _$Status {
const factory Status.success(InitialDataModel model) = Data;
const factory Status.loading() = Loading;
const factory Status.error([String? message]) = Error;
}

然后

[...]
return result.when(
(json) => Status.success(InitialDataModel.fromJson(json)),
error: (e) => Status.error(e),
);
[...]

关于flutter - 卡住如何在顶级模型上分配我自己的 JsonConverter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69704547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com