在我的flutter应用程序中,我需要将文档添加到“ influencerPost”集合中,并且每次用户添加新的“ post”时,都必须将该文档添加到先前的文档中……但是在这一刻,我只是覆盖了先前的文档。正如您通过我的方法所看到的,我有一个uploadImage
将来的机会,它将图像拾取并将其存储到Firebase中的Storage中。之后,我创建了一个称为的集合 influencerPost
,每次新帖子生成新文档时都必须添加而不会覆盖
这是完整的代码:
Future uploadImage(BuildContext context) async {
String fileName = basename(_postImage.path);
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child('postImage/$fileName');
UploadTask uploadTask = firebaseStorageRef.putFile(_postImage);
TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
await FirebaseFirestore.instance
.collection('influencerPost')
.doc(firebaseUser.uid)
.set({
"imageUrl": downloadUrl,
'postText': IfUserProfile.post
});
setState(() => CircularProgressIndicator());
}
这样,我得到了所有文件,但文件被覆盖了,取而代之的是我需要保留前一个文件并添加一个新文件。
由于您要针对用户维护多个帖子,因此必须创建子集合。检查下面的代码,我在其中添加了子集合.collection(“ post”)
Future uploadImage(BuildContext context) async {
String fileName = basename(_postImage.path);
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child('postImage/$fileName');
UploadTask uploadTask = firebaseStorageRef.putFile(_postImage);
TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
await FirebaseFirestore.instance
.collection('influencerPost')
.doc(firebaseUser.uid)
.collection("post")
.add({
"imageUrl": downloadUrl,
'postText': IfUserProfile.post
});
setState(() => CircularProgressIndicator());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句