我有一份来自Firestore的文档列表(postItemz)。我希望将这些信息传递到用于保存数据的模型(发布)中
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});
}
我想将“ postItemz” firestore集合返回的字段传递给List对象?
答案 0 :(得分:0)
希望您正在尝试解析单个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>()
;
}
获取文档快照并解析
var postItemz = await Firestore.instance.collection('posts').document('post-items').get();
var data = Post.fromSnapshot(postItemz );
解析多个文档
使用getDocuments()方法获取文档快照并进行解析
var newData = snapShotdata.documents.map((snapshot) {
return Post.fromSnapshot(snapshot);
}).toList();
希望有帮助!