2021-06-28

Get multiple Image download URLs from Firebase Storage- I can successfully upload multiple images

I am trying to upload multiple images to firebase storage at once. I can upload successfully but cannot retrieve the download URLs;

Future<void> saveImages() async {
    int index = images.length;
    setState(() {
      isLoading = true;
    });

    try {
      final ref = FirebaseStorage.instance
          .ref()
          .child('images/${FirebaseAuth.instance.currentUser!.uid}');
      images.forEach((element) async {
        final file = await element.file;
        final refPut = ref.child('$element$index');
        await refPut.putFile(
          File(file!.path),
        );
      });
      final ref2 = ref.child('${imagePciker.path.split('/')}');
      await ref2.putFile(File(imagePciker.path));
      final ListResult listData = await ref.listAll();
      final data =await ref2.getDownloadURL();
      print('Im HERE!=Line 95');
      Future.forEach(listData.items, (Reference element) async {
        await element.getDownloadURL().then((value) => listUrls.add(value));
        print('line 101');
        print(listUrls.length);
      });

      print('Line 104');
      await FirebaseFirestore.instance.collection('test').add({
        'titleDownloadUrl': data,
        'ListOfDownloadUrl': listUrls,
      });
      print('Line 108');
      setState(() {
        isLoading = false;
      });
    } catch (e) {
   /*    setState(() {
        isLoading = false;
      }); */
      print(e);
    }
  }

The print statements are to debug. This whole function returns and no errors are thrown. However in the firebase collection 'test' the URLs are not stored correctly.

On the first/fresh run after restart of the app. the array of ListOfDownloadUrl is empty

On a hot restart(with the no images saved in firebase storage) the array of ListOfDownloadUrl has one URL

then on multiple restarts, the amount of URLs saved becomes huge(ie. 4 images uploaded-10 URLs saved).

Please comment if I need to add anything else; Thanks for any Help



from Recent Questions - Stack Overflow https://ift.tt/35TcG3Z
https://ift.tt/eA8V8J

No comments:

Post a Comment