Firebase 如何将文档字段从Firestore导入模型字段
我有一份来自Firestore(postItemz)的文档列表。我希望将这些数据传递到为保存数据而构建的模型(Post)中Firebase 如何将文档字段从Firestore导入模型字段,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我有一份来自Firestore(postItemz)的文档列表。我希望将这些数据传递到为保存数据而构建的模型(Post)中 class PostViewModel { List<Post> postItems; PostViewModel({this.postItems}); DocumentReference postItemz = Firestore.instance.collection('posts').document('post-items'); } cl
class PostViewModel {
List<Post> postItems;
PostViewModel({this.postItems});
DocumentReference postItemz = Firestore.instance.collection('posts').document('post-items');
}
class Post {
String personName;
int commentsCount;
List<String> photos;
Post(
{this.personName,
this.commentsCount,
this.photos});
}
class PostViewModel{
列出职位;
PostViewModel({this.positems});
DocumentReference positemz=Firestore.instance.collection('posts')。document('post-items');
}
班岗{
字符串personName;
国际评论员;
列出照片;
职位(
{this.personName,
这是我的朋友,
这张照片});
}
我想将“postItemz”firestore集合返回的字段传递到列表对象中?希望您正在尝试解析单个firestore文档 定义你的博士后课程如下
class Post {
String documentID;
String personName;
int commentsCount;
List<String> photos;
Post.fromSnapshot(DocumentSnapshot snapshot)
: documentID = snapshot.documentID,
personName = snapshot['personName'],
commentsCount = snapshot['commentsCount'],
photos = snapshot['photos'].cast<String>()
;
}
解析多个文档的步骤
使用getDocuments()方法获取文档快照并进行解析
var postItemz = await Firestore.instance.collection('posts').document('post-items').get();
var data = Post.fromSnapshot(postItemz );
var newData = snapShotdata.documents.map((snapshot) {
return Post.fromSnapshot(snapshot);
}).toList();
希望有帮助
positemz
不是文档列表。它是对您尚未查询的单个文档的引用。您必须查询数据库,如文档中所述。从您的链接中,哪种语言引用与flatter-Dart匹配:Java、Web等。ThanksAll平台在异步获取文档的文档引用上有一个get()方法。谢谢谢谢你的帮助。我已经实现了这些更改,但是在上面代码块2的第2行中的变量“positemz”上出现了以下错误:参数类型“Future”无法分配给参数类型“DocumentSnapshot”。dart(参数类型不可分配)
实际上,我正在尝试分析集合中的多个文档。请在调用之前添加wait,并在异步方法中进行调用