0
votes

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
Thanks Claudio but i already tried this one but for the new update of firebase storage most of them are deprecated. Thanls for your timeJude Harshan

1 Answers

0
votes

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