gpt4 book ai didi

firebase - 如何在Flutter中与类 “flutter pub run build_runner build”具有字段Timestamp?

转载 作者:行者123 更新时间:2023-12-03 04:53:09 25 4
gpt4 key购买 nike

我正在尝试使用cmd“flutter pub run build_runner build”从对象序列化JSON格式,但它始终显示以下消息:

运行JsonSerializableGenerator时出错
无法为fromJson生成timeTaking代码。
提供的TypeHelper实例均不支持定义的类型。

@JsonSerializable(explicitToJson: true)
class RequestData {
@JsonKey(required: true)
String uid;
String phone;
String email;
String description;
/// Tell json_serializable that "owner_name" should be
/// mapped to this property.
@JsonKey(name: 'owner-name')
String ownerName;
@JsonKey(name: 'location-detail')
String locationDetail;
int status;// 0 waiting, 1 accepted, 2 done, 3 DOING , 4 cancel
List<String> imageUrls;
@JsonKey( required: true, name: "time-taking", toJson: _timestampToJson/*,fromJson: _timeStampFromJson*/)
Timestamp timeTaking;

RequestData({this.uid, this.phone, this.email, this.description,
this.ownerName, this.locationDetail, this.status, this.imageUrls,this.timeTaking});

RequestData.none();
}

我该如何解决这个问题?

最佳答案

我有同样的问题,我的临时解决方案是创建一个JsonConverter转换为DateTime:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:json_annotation/json_annotation.dart';

class TimestampConvertDatetime implements JsonConverter<DateTime, Timestamp> {
const TimestampConvertDatetime();
@override
DateTime fromJson(Timestamp json) {
return json.toDate();
}

@override
Timestamp toJson(DateTime object) {
return Timestamp.fromDate(object);
}
}

并应用于您的模型:
...
@TimestampConvertDatetime()
DateTime date;
....

关于firebase - 如何在Flutter中与类 “flutter pub run build_runner build”具有字段Timestamp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61056945/

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