dart 如何使用Flutter在Firestore中存储多个URL

bwitn5fc  于 2023-11-14  发布在  Flutter
关注(0)|答案(1)|浏览(114)

我有一个名为Upload的屏幕,用户可以通过输入描述和多个图像上传帖子,并在主页上检索Firestore的数据。
问题是,在上传屏幕中,当我按下将图像及其URL上传到Firebase存储时,存储多个图像没有问题,但在Firestore中存储URL有问题。


的数据
我试过了:

Widget postButton(){
    return RaisedButton(
      shape:RoundedRectangleBorder( borderRadius: BorderRadius.circular(15.0),),
      color: Color.fromRGBO(0,21,43,1),
      onPressed:  () {
              uploadToFirebase();
              UploadPost();
      },
      textColor: Colors.white,
      padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
      child: Container(
        alignment: Alignment.center,
        width: _width/1.7,
        child: Text(
          "Upload Post",
          style: TextStyle(
            fontSize: 16.0,
          ),
        ),
      ),
    );
  }

  void UploadPost() async{
   UserManagement().addPost(Name,Des,UserImageUrl,ImageUrl,PhoneNumber, context);
  }

  void uploadToFirebase() async{
    _paths.forEach((fileName, filePath) => {upload(fileName, filePath)});
  }

  void upload(fileName, filePath) async{
    DateTime date = new DateTime.now();
    var Date = DateFormat('EEE d MMM kk:mm:ss').format(date);
    StorageReference storageRef = FirebaseStorage.instance.ref().child("User Posts").child('$PhoneNumber').child('$Date').child('$date');
    final StorageUploadTask uploadTask = storageRef.putFile(File(filePath));
    var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    var url = downUrl.toString();
    print(url);
    setState(() {
      ImageUrl = url;
      print("Done");
      print(ImageUrl);
    });

  }

字符串

mwngjboj

mwngjboj1#

获取url后,您可以在firestore中存储:

var url = downUrl.toString();
    print(url);
Firestore.instance.collection("Posts").document("doc_id").setData(
  {
   "image Urls" : url,
  },merge : true).then((_){
      print("success!");
  });

字符串
这将把url添加到文档中

相关问题