相关疑难解决方法(0)

如何解决类型“时间戳”不是类型转换中“字符串”类型的子类型

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

type 'Timestamp' is not a subtype of type 'String' in type cast
Run Code Online (Sandbox Code Playgroud)

当我进入 generate 时meeting.g.dart,正是这一行导致了错误

json['date'] == null ? null : DateTime.parse(json['date'] as String)
Run Code Online (Sandbox Code Playgroud)

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

Error running JsonSerializableGenerator
Could not generate `fromJson` code for `date`.
None of the …
Run Code Online (Sandbox Code Playgroud)

dart flutter google-cloud-firestore

10
推荐指数
3
解决办法
3214
查看次数

标签 统计

dart ×1

flutter ×1

google-cloud-firestore ×1