Json 在dart中将MAP毫秒历元转换为日期时间
我将日期时间存储为firebase json中的毫秒周期,然后在调用fromMap时,我希望从毫秒周期转换为毫秒周期 除了我目前的工作之外,哪种方式最合适Json 在dart中将MAP毫秒历元转换为日期时间,json,datetime,angular,firebase,dart,Json,Datetime,Angular,Firebase,Dart,我将日期时间存储为firebase json中的毫秒周期,然后在调用fromMap时,我希望从毫秒周期转换为毫秒周期 除了我目前的工作之外,哪种方式最合适 new DateTime.froms毫秒新纪元(map['created\u at']) 消息类 class Message { final String name; final String text; String photoURL; String imageURL; DateTime created_at; M
new DateTime.froms毫秒新纪元(map['created\u at'])
消息类
class Message {
final String name;
final String text;
String photoURL;
String imageURL;
DateTime created_at;
Message(this.name, this.created_at, [this.text, String photoURL, this.imageURL]) {
this.photoURL = photoURL ??
"https://lh3.googleusercontent.com/"
}
Message.fromMap(Map map) :
this(map['name'], new DateTime.fromMillisecondsSinceEpoch(map['created_at']), map['text'], map['photoURL'], map['imageURL']);
Map toMap() =>
{
"name": name,
"created_at": created_at.millisecondsSinceEpoch,
"text": text,
"photoURL": photoURL,
"imageURL": imageURL
};
}
我觉得你的代码不错
我能看到的关于当前代码的唯一潜在问题是,它在本地时区中创建了DateTime
对象。如果您希望将其作为时间戳,您可能希望始终使用UTC时间,因此您可能希望:
new DateTime.froms毫秒新纪元(map['created_at'],isUtc:true)
同样,如果向用户显示日期,使用本地时间可能是正确的做法
另一个潜在的问题是映射可能包含一个字符串而不是一个数字,但是这里的示例中有一个数字,所以这可能不是问题。只需确保您知道JSON包含什么
(但为了挑剔,在成员处创建的通常会在Dart中写为createdAt
。
我觉得你的代码不错
我能看到的关于当前代码的唯一潜在问题是,它在本地时区中创建了DateTime
对象。如果您希望将其作为时间戳,您可能希望始终使用UTC时间,因此您可能希望:
new DateTime.froms毫秒新纪元(map['created_at'],isUtc:true)
同样,如果向用户显示日期,使用本地时间可能是正确的做法
另一个潜在的问题是映射可能包含一个字符串而不是一个数字,但是这里的示例中有一个数字,所以这可能不是问题。只需确保您知道JSON包含什么
(但要吹毛求疵,在
成员处创建的通常会在Dart中写为createdAt
。您现在使用的方式有什么问题?您现在使用的方式有什么问题?