我如何解析类型'Timestamp'不是类型转换中类型'String'的子类型 [英] How do I resolve type 'Timestamp' is not a subtype of type 'String' in type cast

查看:84
本文介绍了我如何解析类型'Timestamp'不是类型转换中类型'String'的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Firestore中获取会议并将其映射到以下 Meeting 模型中:

I want to fetch meetings from Firestore and map them into the following Meeting model:

part 'meeting.g.dart';

@JsonSerializable(explicitToJson: true)
class Meeting {
  String id;
  DateTime date;

  Meeting(this.id, this.date);

  factory Meeting.fromJson(Map<String, dynamic> json) {

    return _$MeetingFromJson(json);
  }

  Map<String, dynamic> toJson() => _$MeetingToJson(this);
}

从Firestore获取文档,然后在可迭代对象上调用 fromJson ,但会引发异常:

The documents are fetched from Firestore and then fromJson is called on the iterable, but an exception is thrown:

type 'Timestamp' is not a subtype of type 'String' in type cast

当我进入生成的 meeting.g.dart 时,正是这一行导致错误

When I go into generated meeting.g.dart, it's this line which causes the error

json['date'] == null ? null : DateTime.parse(json['date'] as String)

要解决此问题,我尝试将模型中的DateTime更改为Timestamp,但随后显示以下构建错误:

To workaround the issue, I've tried changing from DateTime to Timestamp in the model, but then the following build error is shown:

Error running JsonSerializableGenerator
Could not generate `fromJson` code for `date`.
None of the provided `TypeHelper` instances support the defined type.

你能告诉我如何解决这个问题吗?是否还有另一种首选方法将Firebase和Flutter项目结合使用json_serializable进行JSON序列化?甚至可以替换json_serializable的用法?

Could you tell me how do you solve this issue? Is there another preferred way to combine Firebase and a Flutter project using json_serializable for JSON serialization? Maybe even replace usage of json_serializable ?

推荐答案

解决方案#1

使用 toJson fromJson 转换器功能,如以下示例所示:

Solution #1

Use toJson and fromJson converter functions as in the following example: https://github.com/dart-lang/json_serializable/blob/master/example/lib/example.dart

该解决方案的优点是您不必对属性名称进行硬编码

Benefits of the solution is that you don't have to hard-code property names

阅读> https://github.com/dart-lang/json_serializable/issues/351 ,我更改了 Meeting.fromJson ,它现在可以按预期工作了:

After reading https://github.com/dart-lang/json_serializable/issues/351, I've changed Meeting.fromJson and it works as expected now:

  factory Meeting.fromJson(Map<String, dynamic> json) {
    json["date"] = ((json["date"] as Timestamp).toDate().toString());
    return _$MeetingFromJson(json);
  }

json ["date"] 默认为 Timestamp ,在到达生成的解串器之前,我将其转换为 String .当尝试将 json ["date"]]转换为字符串

json["date"] is Timestamp by default, I convert it to String, before it reaches the generated deserializer, so it doesn't crash when it tries to cast json["date"] as String

尽管,我不太喜欢这种解决方法,因为我必须对属性的名称和类型进行硬编码,但是就目前而言,这种解决方案已经足够了.

Though, I don't like this workaround very much, because I have to hard-code property's name and couple to types, but for now, this solution will be good enough.

另一种方法是尝试 https://pub.dev/packages/built_value serialiazion,在他们的博客中推荐 https://flutter.dev/docs/开发/数据和后端/json

An alternative would be to try out https://pub.dev/packages/built_value for serialiazion, which is recommended in their blog https://flutter.dev/docs/development/data-and-backend/json

这篇关于我如何解析类型'Timestamp'不是类型转换中类型'String'的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆