Flutter: How Can I Get URL From Firebase Storage In Firestore

前端 未结 1 1403
星月不相逢
星月不相逢 2021-01-28 01:51

I want to upload the image to firebase storage in want to store the download URL in the firestore. I have the code that I have bellowed! but the problem is whenever I try to upl

1条回答
  •  日久生厌
    2021-01-28 02:12

    here is how you can implement it

              final StorageReference storageReference =
                    FirebaseStorage.instance.ref().child('images/$name');
                StorageUploadTask uploadTask = storageReference.putData(asset);
    
                final StreamSubscription streamSubscription =
                    uploadTask.events.listen((event) {
                  // You can use this to notify yourself or your user in any kind of way.
                  // For example: you could use the uploadTask.events stream in a StreamBuilder instead
                  // to show your user what the current status is. In that case, you would not need to cancel any
            // subscription as StreamBuilder handles this automatically.
         });
             await uploadTask.onComplete;
                streamSubscription.cancel();
    //get your ref url    
    
                String docUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    //docUrl is your url as a string
                Firestore.instance
                    .collection('collectiion')
                    .document()
            .updateData({"url": docUrl});
    

    0 讨论(0)
提交回复
热议问题