我正在获取数据云存储,并尝试使用以下代码在我的应用中显示。
new Text(timeago.format(document.data['tripDoc']['docCreatedOn'])),
Run Code Online (Sandbox Code Playgroud)
我正在使用timeagodart包进行格式化。但是,在更新到最新的Cloud Firestore插件后,出现此错误-
Another exception was thrown: type 'Timestamp' is not a subtype of type 'DateTime'
Run Code Online (Sandbox Code Playgroud)
无法理解如何将“ TimeStamp”对象解析为“ DateTime”。因为timeago插件需要DateTime对象格式的数据。
小智 19
添加 toDate() 方法。它会起作用
DateTime dateTime = documents[i].data["duedate"].toDate();
Run Code Online (Sandbox Code Playgroud)
Sur*_*esh 18
.toDate()为我工作。现在修改后的代码是-
new Text(timeago.format(document.data['tripDoc']['docCreatedOn'].toDate()))
Run Code Online (Sandbox Code Playgroud)
希望能对某人有所帮助。
我觉得这个更靠谱
DateTime updateDateTime = DateTime.fromMillisecondsSinceEpoch(
map['updatedatetime'].millisecondsSinceEpoch);
Run Code Online (Sandbox Code Playgroud)
iOS和Android将不会收到相同的类型。ios已将时间戳作为TimeStamp接收,而Android已将其接收为DateTime。因此,为了解决此问题,我刚刚创建了这个小功能。这将返回一个DateTime并让其格式化等。
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
DateTime parseTime(dynamic date) {
return Platform.isIOS ? (date as Timestamp).toDate() : (date as DateTime);
}
Run Code Online (Sandbox Code Playgroud)
如果您使用JsonSerializable,请使用JsonConverter
class TimestampConverter implements JsonConverter<DateTime, Timestamp> {
const TimestampConverter();
@override
DateTime fromJson(Timestamp timestamp) {
return timestamp.toDate();
}
@override
Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
}
@JsonSerializable()
class User{
final String id;
@TimestampConverter()
final DateTime timeCreated;
User([this.id, this.timeCreated]);
factory User.fromSnapshot(DocumentSnapshot documentSnapshot) =>
_$UserFromJson(
documentSnapshot.data..["_id"] = documentSnapshot.documentID);
Map<String, dynamic> toJson() => _$UserToJson(this)..remove("_id");
}
Run Code Online (Sandbox Code Playgroud)
DateTime.fromMillisecondsSinceEpoch(timeStamp);
DateTime.fromMicrosecondsSinceEpoch(timeStamp);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5694 次 |
| 最近记录: |