1

我冻结了模型(简化):

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
return 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),
    );
  }
}

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

这一行引发了我上面提到的错误

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

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

4

1 回答 1

2

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

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

  ...

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

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


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


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

@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),
    );
[...]

于 2021-10-25T14:11:47.577 回答