我正在創建一個社交媒體型別應用程式,此功能會公開創建一個帖子,供應用程式上的每個人查看
static void createPost(Post post) {
postsRef.doc(post.authorId).set({'postTime': post.timestamp});
postsRef.doc(post.authorId).collection("userPosts").add({
"text": post.text,
"image": post.image,
"authorId": post.authorId,
"timestamp": post.timestamp,
"likes": post.likes
}).then((doc) {
QuerySnapshot homeSnapshot =
usersRef.doc(post.authorId).collection('users').get();
for (var docSnapshot in homeSnapshot.docs) {
feedRefs.doc(docSnapshot.id).collection('userFeed').doc(doc.id).set({
"text": post.text,
"image": post.image,
"authorId": post.authorId,
"timestamp": post.timestamp,
"likes": post.likes
});
}
});
}
錯誤部分是
QuerySnapshot homeSnapshot =
usersRef.doc(post.authorId).collection('users').get();
如果需要,還可以在函式中傳遞 Post 模型:
import 'package:cloud_firestore/cloud_firestore.dart';
class Post {
String id;
String authorId;
String text;
String image;
Timestamp timestamp;
int likes;
Post({
required this.id,
required this.authorId,
required this.text,
required this.image,
required this.timestamp,
required this.likes,
});
factory Post.fromDoc(DocumentSnapshot doc) {
return Post(
id: doc.id,
authorId: doc['authorId'],
text: doc['text'],
image: doc['image'],
timestamp: doc['timestamp'],
likes: doc['likes'],
);
}
}
那么顯然錯誤在標題中,非常感謝任何幫助,謝謝:D
uj5u.com熱心網友回復:
您應該await使用異步功能。所以改變這個:
QuerySnapshot homeSnapshot =
usersRef.doc(post.authorId).collection('users').get();
對此:
QuerySnapshot homeSnapshot =
await usersRef.doc(post.authorId).collection('users').get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/504040.html
標籤:Google Cloud Collective 数据库 扑 火力基地 谷歌云火库
上一篇:如何離線存盤資訊。C#,統一
