How to save the Image to Firebase storage and save the URL to firebase firestore?

梦想与她 提交于 2021-01-07 01:07:48

问题


Hi I am trying to save an image which is picked from gallery or camera.

Future<void> getImage({ImageSource source}) async {
    _pickedImage = await ImagePicker().getImage(source: source);
    if (_pickedImage != null) {
      _pickedImageFile = File(_pickedImage.path);
    }
  }

After picking the image it has to update image to the firebase storage.

void _uploadImage(File image) async {
    Reference storageReference = FirebaseStorage.instance
        .ref()
        .child('UserImage')
        .child("UserImage/${user.uid}");
    UploadTask uploadTask = storageReference.putFile(image);

    //StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    uploadTask.then((newUrl) async {
      newUrl.ref.getDownloadURL();
      return imageUrl = newUrl.toString();
    }).catchError((onError) {
      print(onError);
    });

  }

The issue is the code is breaking at uploadTask.then((newUrl) and not letting to get the downloadURL as a String. Then it has to update the image path to the firestore collection.

void userDetailsUpdate() {
    FirebaseFirestore.instance
        .collection("Manufacturer-Accounts")
        .doc(user.uid)
        .update({
      'Designation': designation.text,
      'FirstName': firstName.text,
      'LastName': lastName.text,
      //'Email': user.email,
      'Phone': phoneNumber.text,
      'UserImage': imageUrl == null ? "" : imageUrl,
    });
  }

Because it is not getting the image url so it is saving the value as "". Is there a way of doing it? I have used the Firebase storage ^5.0.1

My full source code is given here How can I add a new value to my text editcontroller in Text formfield?


回答1:


Your _uploadImage function is not returning anything, so whoever calls it never gets the download URL.

Future<String> _uploadImage(File image) async {
    Reference storageReference = FirebaseStorage.instance
        .ref()
        .child('UserImage')
        .child("UserImage/${user.uid}");
    UploadTask uploadTask = storageReference.putFile(image);

    await Future.value(uploadTask);

    var newUrl = await storageReference.getDownloadURL();

    return newUrl;
}

Now you can call this function and get the download URL with:

var downloadURL = await _uploadImage(file);


来源:https://stackoverflow.com/questions/65107991/how-to-save-the-image-to-firebase-storage-and-save-the-url-to-firebase-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!