如何从类中获取映射中Firestore中的服务器时间



从类实例添加到Firestore

FirebaseFirestore.instance.collection('collection').add({'history':History(FieldValue.serverTimestamp(), 'action', 'who').makeMap()}).then((value) {
// ...
});

时间字段应该有什么类型?

class History{
FieldValue time  = FieldValue.serverTimestamp();
String action = '';
String who = '';
var history =  <FieldValue, Map<String, String>>{};
History(FieldValue time, String action, String who){
this.time = time;
this.action = action;
this.who = who;
}
Map<FieldValue, Map<String, String>> makeMap(){
var tempMap = <String, String>{
'action' : action,
'who' : who,
};
history[time] = tempMap;
return history;
}
}

如何获得这种形式的字符串?

{"time" : 'October 25, 2022 at 10:44:39 PM UTC+1' ,
{ "action": "action", "who": "who" }};

尝试将时间值存储在timeStamp类型中,以及何时保存在timeStamp中,然后可以轻松地转换为字符串

最新更新