Flutter 如何在Flatter中使用json_serializable反序列化Firestore文档及其id?
我的Firestore数据库中有一个简单的消息文档,其中包含一些字段 我使用Flutter 如何在Flatter中使用json_serializable反序列化Firestore文档及其id?,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,我的Firestore数据库中有一个简单的消息文档,其中包含一些字段 我使用json_serializable将其反序列化为对象。我的班级情况如下: import'包:cloud_firestore/cloud_firestore.dart'; 导入“包:equalable/equalable.dart”; 导入“package:json_annotation/json_annotation.dart”; “message_firestore.g.dart”部分; @JsonSerializa
json_serializable
将其反序列化为对象。我的班级情况如下:
import'包:cloud_firestore/cloud_firestore.dart';
导入“包:equalable/equalable.dart”;
导入“package:json_annotation/json_annotation.dart”;
“message_firestore.g.dart”部分;
@JsonSerializable(null:true,explicitToJson:true)
MessageFirestore类扩展了equalable{
MessageFirestore(
this.id、this.content、this.conversationId、this.senderId、this.dateSent);
factory MessageFirestore.fromJson(映射json)=>
_$MessageFirestoreFromJson(json);
映射到JSON()=>$MessageFirestoreToJson(这个);
@JsonKey(名称:“Id”)
最终字符串id;
@JsonKey(名称:“内容”)
最终字符串内容;
@JsonKey(名称:“会话ID”)
最后一个字符串conversationId;
@JsonKey(名称:“SenderId”)
最终字符串senderId;
@JsonKey(名称:“DateSent”,fromJson:\u fromJson,toJson:\u toJson)
最终日期时间日期发送;
静态日期时间\u fromJson(时间戳val)=>
DateTime.From毫秒新纪元(val.毫秒新纪元);
静态时间戳\u toJson(日期时间)=>
Timestamp.fromsmillissecondssinceepoch(time.millissecondssinceepoch);
}
文档中没有名为Id
的字段,因此当前未对其Id进行反序列化。
但是,从Firestore检索到的映射的键是其id,因此可以通过手动反序列化映射来读取该值。
我希望在反序列化期间能够访问文档的id(_b03002…)
有没有办法将json_serializable
配置为读取此id并将其存储在id
属性中?您可以修改fromJson
构造函数,以便在第一个参数上提供id
factory MessageFirestore.fromJson(字符串id,映射json){
返回$MessageFirestoreFromJson(json)…id=id;
}
然后,从你的来电者那里,会是这样的
消息(snapshot.documentID、snapshot.data)
您可以修改来自JSON的构造函数,以便在第一个参数上提供id
factory MessageFirestore.fromJson(字符串id,映射json){
返回$MessageFirestoreFromJson(json)…id=id;
}
然后,从你的来电者那里,会是这样的
消息(snapshot.documentID、snapshot.data)
您可以将另一个工厂添加到MessageFirestore类中
factory MessageFirestore.fromFire(DocumentSnapshot doc) =>
_$MessageFirestoreFromFire(doc);
之后,在类中将有两个工厂函数
factory MessageFirestore.fromFire(DocumentSnapshot doc) //...
factory MessageFirestore.fromJson(Map<String, dynamic> json) //...
在阅读文件的过程中,您可以:
Stream<List<MessageFirestore>> getMessageList() {
return Firestore.instance
.collection('YourCollectionPath')
.snapshots()
.map((snapShot) => snapShot.documents
.map(
(document) => MessageFirestore.fromFire(document),
)
.toList());
}
Stream getMessageList(){
返回Firestore.instance
.collection('YourCollectionPath')
.快照()
.map((快照)=>snapShot.documents
.地图(
(文档)=>MessageFirestore.fromFire(文档),
)
.toList());
}
容易的豌豆
而且该方法不会干扰使用MessageFirestore实例的其他类
祝你度过愉快的一天,并希望此方法对你有效。:) 您可以将另一个工厂添加到MessageFirestore类中
factory MessageFirestore.fromFire(DocumentSnapshot doc) =>
_$MessageFirestoreFromFire(doc);
之后,在类中将有两个工厂函数
factory MessageFirestore.fromFire(DocumentSnapshot doc) //...
factory MessageFirestore.fromJson(Map<String, dynamic> json) //...
在阅读文件的过程中,您可以:
Stream<List<MessageFirestore>> getMessageList() {
return Firestore.instance
.collection('YourCollectionPath')
.snapshots()
.map((snapShot) => snapShot.documents
.map(
(document) => MessageFirestore.fromFire(document),
)
.toList());
}
Stream getMessageList(){
返回Firestore.instance
.collection('YourCollectionPath')
.快照()
.map((快照)=>snapShot.documents
.地图(
(文档)=>MessageFirestore.fromFire(文档),
)
.toList());
}
容易的豌豆
而且该方法不会干扰使用MessageFirestore实例的其他类
祝你度过愉快的一天,并希望此方法对你有效。:) 对的帖子的改进:同时删除toJson上的id
:
factory MessageFirestore.fromJson(String id, Map<String, dynamic> json) {
return _$MessageFirestoreFromJson(json)..id = id;
}
Map<String, dynamic> toJson() {
var json = _$MessageFirestoreToJson(this);
json.removeWhere((key, value) => key == 'id');
return json;
}
factory MessageFirestore.fromJson(字符串id,映射json){
返回$MessageFirestoreFromJson(json)…id=id;
}
映射到JSON(){
var json=$MessageFirestoreToJson(这个);
removeWhere((key,value)=>key='id');
返回json;
}
对的帖子的改进:同时删除toJson上的id
:
factory MessageFirestore.fromJson(String id, Map<String, dynamic> json) {
return _$MessageFirestoreFromJson(json)..id = id;
}
Map<String, dynamic> toJson() {
var json = _$MessageFirestoreToJson(this);
json.removeWhere((key, value) => key == 'id');
return json;
}
factory MessageFirestore.fromJson(字符串id,映射json){
返回$MessageFirestoreFromJson(json)…id=id;
}
映射到JSON(){
var json=$MessageFirestoreToJson(这个);
removeWhere((key,value)=>key='id');
返回json;
}
>然而,从Firestore检索到的地图的关键是它的id。。。。你在说什么?一张地图有很多键。我所说的地图的key
是指屏幕截图上的b0300…
id。我发现它在DocumentSnapshot
中作为属性documentID
提供。我希望在反序列化对象时可以访问此id。我将在我的问题中澄清这一点。>然而,从Firestore检索到的地图的关键是它的id。。。。你在说什么?一张地图有很多键。我所说的地图的key
是指屏幕截图上的b0300…
id。我发现它在DocumentSnapshot
中作为属性documentID
提供。我希望在反序列化对象时可以访问此id。我想在我的问题中澄清一下。嗨,那是什么。。打电话来了?谢谢Yaobin@YaobinThen如果类是Message的属性,那么在(json_serializable的上下文)中是否可以这样做?不确定我是否理解正确,类是Message的属性?也许您想将documentID
的传递封装在Message类中<代码>工厂消息.fromDocumentSnapshot(DocumentSnapshot snapshot)=>\u$MessageFromJson(snapshot.data)…id=snapshot.documentID嗨,那是什么。。打电话来了?谢谢Yaobin@YaobinThen有没有可能在家里这样做