0

I'm facing the issue, when I want to upload multiple images. I have a List that holds the 'File'-s. I read more article about it, and found this:

Future<List> uploadImage(List<Object> _imageFile) async {
    final user = await FirebaseAuth.instance.currentUser();
    List _urllist = [];

    int i = 0;
    await _imageFile.forEach((image) async {
      print('igen: ' + image.toString());
      
        // if (ref.getMetadata() != null) {   //should be deleted, but this isn't working
        //   await ref.delete();
        // }
        print(image.imageFile);
        final ref = FirebaseStorage.instance
            .ref()
            .child('business_image')
            .child(user.uid)
            .child(i.toString() + '.jpg');
        i++;
        StorageUploadTask uploadTask = ref.putFile(image.imageFile);
        StorageTaskSnapshot downloadUrl = await uploadTask.onComplete;
        String _url = await downloadUrl.ref.getDownloadURL();
        _urllist.add(_url);
      
    });
    print(_urllist);

    return _urllist;
}

But when I'm calling this function:

List imageurllist;
final user = await FirebaseAuth.instance.currentUser();
uploadImage(getnewphotos).then((List urls) {
  imageurllist = urls;
  print(urls); //here I try to print it, but it returns null, guess the function don't wait for it?
  Firestore.instance.collection('users').document(user.uid).updateData({
    'email': widget.userData['email'],
    'username': userName != null ? userName : widget.userData['username'],
    'usertype': widget.userData['usertype'],
    'loclat': loclat != null ? loclat : widget.userData['loclat'],
    'loclng': loclng != null ? loclng : widget.userData['loclng'],
    'locationread':
        searchAddr != null ? searchAddr : widget.userData['locationread'],
    'services': serviceList,
    'opening': openstart != null ? openstart : widget.userData['opening'],
    'closing': closeend != null ? closeend : widget.userData['closing'],
    'userImage': imageurllist,
  });

  setState(() {
    isUpload = false;
  });
});

What is the problem with this function, can someone please help me solve it?

2
  • What error do you see Commented Aug 15, 2020 at 19:21
  • there is no error, it just simply return null. so I upload an empty list to firebase.The problem is with the return, the function returns before it could produce all of the imageurls. Commented Aug 15, 2020 at 21:44

1 Answer 1

1

From what i have done is that when the user selects or takes a pic from their phone it will add the image to List<Object> images' which im sure you may have implemented yourself.

In regards to uploading those images, what i have done is that it uploads the images in batch to firebase storage and then uses the url for each user if that is what you are after then here is my code.

  Future handleUploadImage() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();

    var uuid = new Uuid();

    try {
      for (int i = 0; i < widget.images.length; i++) {
        final StorageReference storageRef =
            FirebaseStorage.instance.ref().child(uuid.v4());
        final StorageUploadTask task = storageRef
            .child('user_images/${firebaseUser.uid}/')
            .putFile(widget.images[i]);
        await task.onComplete.then((picValue) async {
          await picValue.ref.getDownloadURL().then((downloadUrl) {
            print("URL : " + downloadUrl);
            imageUrl.add(downloadUrl);
          });
        });
      }
    } catch (e) {
      print(e);
    }
  }

Bare in mind i user uuid package to produce different image urls

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand it why, but it doesn't even print the URL, my imageUrl list is still empty...
I'm calling your function this way: handleUploadImage().then((value) { Firestore.instance.collection('users').document(user.uid).updateData({ 'email': widget.userData['email'], . . . etc 'userImage': allimageurls, }); setState(() { isUpload = false; }); }); In this example your imageUrl is my allimageurl. but the print("URL : " + downloadUrl); never executes
It works for me. Your image array might be empty or there might be an error with how you add the images to the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.