Created
July 30, 2020 16:50
-
-
Save prasadshirvandkar/005be6f077c606863dd4fb518f3da82e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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