Cannot upload multiple images to firebase in a function. #8783
-
I was trying to upload multiple images to firebase storage using the function below. The image selection and files list work perfectly but the upload process is not working. The first image given in the list to the upload function is uploaded to the storage directory, and only its link is returned, when every image selected in the Future mediaUploadToDestination(
{required BuildContext context,
required List files,
required String destination}) async {
List<String> urls = [];
for (File image in files) {
try {
// reference of the storage directory
Reference ref = FirebaseStorage.instance
.ref()
.child(destination + image.path.split('/').last.toString());
await ref.putFile(image).whenComplete(
() async {
await ref.getDownloadURL().then(
(value) {
urls.add(value);
},
);
},
);
} catch (e) {
CustomWidgets().snackBarWidget(
content: Tools().firebaseErrorTextHandling(e.toString()),
context: context);
}
return urls;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved this problem myself. Here's the answer in case anyones wondering.Future mediaUploadToDestination(
{required BuildContext context,
required List files,
required String destination}) async {
List<String> urls = [];
for (File image in files) {
try {
// reference of the storage directory
Reference ref = FirebaseStorage.instance
.ref()
.child(destination + image.path.split('/').last.toString());
await ref.putFile(image).whenComplete(
() async {
await ref.getDownloadURL().then(
(value) {
urls.add(value);
},
);
},
);
} catch (e) {
CustomWidgets().snackBarWidget(
content: Tools().firebaseErrorTextHandling(e.toString()),
context: context);
}
}
return urls;
} What happened?The return statement was in the try indentation block when it should have been in the function block, outside the try block. |
Beta Was this translation helpful? Give feedback.
Solved this problem myself. Here's the answer in case anyones wondering.