Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prasadshirvandkar/005be6f077c606863dd4fb518f3da82e to your computer and use it in GitHub Desktop.
Save prasadshirvandkar/005be6f077c606863dd4fb518f3da82e to your computer and use it in GitHub Desktop.
//Getting Downloaded URI directly
uploadFile(MediaInfo mediaInfo, String ref, String fileName) {
try {
String mimeType = mime(basename(mediaInfo.fileName));
var metaData = UploadMetadata(contentType: mimeType);
StorageReference storageReference = storage().ref(ref).child(fileName);
UploadTask uploadTask = storageReference.put(mediaInfo.data, metaData);
var imageUri;
uploadTask.future.then((snapshot) => {
Future.delayed(Duration(seconds: 1)).then((value) => {
snapshot.ref.getDownloadURL().then((dynamic uri) {
imageUri = uri;
print('Download URL: ${imageUri.toString()}');
})
})
});
} catch (e) {
print('File Upload Error: $e');
}
}
//OR You can return Future Download URI
Future<Uri> uploadFile1(
MediaInfo mediaInfo, String ref, String fileName) async {
try {
String mimeType = mime(basename(mediaInfo.fileName));
var metaData = UploadMetadata(contentType: mimeType);
StorageReference storageReference = storage().ref(ref).child(fileName);
UploadTaskSnapshot uploadTaskSnapshot =
await storageReference.put(mediaInfo.data, metaData).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
print('Download URL: $imageUri');
return imageUri;
} catch (e) {
print('File Upload Error: $e');
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment