0

I have a screen called Upload where a user can upload a post by entering a description and multiple images, and on the homepage I will retrieve data from Firestore.

The problem is, in the upload screen, when I press to upload images and their URL into Firebase storage, there is no problem in storing multiple images, but there is a problem storing the URL in Firestore.

enter image description here

I tried:

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);
    });

  }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sandeep Sharma
  • 639
  • 2
  • 9
  • 34

1 Answers1

1

After getting the url, you can store in firestore:

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

This will add the url to the document

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134