I'm trying to upload n number of photos to Firebase Storage and save those URLs in an array inside Firestore, but I am not able to get the downloadURL()
or I do not know where to find it rather. I've checked other answers but those were for single files, I'm trying to upload a batch and store the URLs together instead of uploading on and storing the URL to Firestore and so on and so forth...
CODE:
_uploadImages(String userID, String productID, List<File> images, Function onSuccess(List<String> imageURLs), Function onFailure(String e)) {
List<String> imageURLs = [];
int uploadCount = 0;
StorageReference storeRef = FirebaseStorage.instance.ref().child('Products').child(userID).child(productID).child(uploadCount);
StorageMetadata metaData = StorageMetadata(contentType: 'image/png');
images.forEach((image) {
storeRef.putFile(image, metaData).onComplete.then((snapshot) {
STUCK AT THIS POINT SINCE THE SNAPSHOT DOESN'T SHOW THE URL OPTION...
//imageURLs.add(snapshot. )
uploadCount++;
if (uploadCount == images.length) {
onSuccess(imageURLs);
}
});
});
}
You could use this method for multiple file upload to firebase storage where List<Asset>
assets are your List<File>
files.
Future<List<String>> uploadImage(
{@required String fileName, @required List<Asset> assets}) async {
List<String> uploadUrls = [];
await Future.wait(assets.map((Asset asset) async {
ByteData byteData = await asset.requestOriginal();
List<int> imageData = byteData.buffer.asUint8List();
StorageReference reference = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = reference.putData(imageData);
StorageTaskSnapshot storageTaskSnapshot;
// Release the image data
asset.releaseOriginal();
StorageTaskSnapshot snapshot = await uploadTask.onComplete;
if (snapshot.error == null) {
storageTaskSnapshot = snapshot;
final String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
uploadUrls.add(downloadUrl);
print('Upload success');
} else {
print('Error from image repo ${snapshot.error.toString()}');
throw ('This file is not an image');
}
}), eagerError: true, cleanUp: (_) {
print('eager cleaned up');
});
return uploadUrls;
}
These solutions does not work with firebase_storage: ^5.0.1 as
await uploadTask.onComplete;
is not applicable anymore. Please suggest any other solution if you upgraded your app. Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With