uploadFile method
Upload PickedFile to Cloud Storage and get file url
Implementation
Future<String> uploadFile(PickedFile orFile) async {
String imageUrl = '';
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
firebase_storage.Reference reference =
firebase_storage.FirebaseStorage.instance.ref().child(fileName);
late File compressedFile;
if (!kIsWeb)
compressedFile = await FlutterNativeImage.compressImage(orFile.path,
quality: 80, percentage: 90);
try {
firebase_storage.UploadTask uploadTask;
final metadata = firebase_storage.SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': orFile.path});
if (kIsWeb)
uploadTask = reference.putData(await orFile.readAsBytes(), metadata);
else
uploadTask = reference.putFile(compressedFile);
firebase_storage.TaskSnapshot storageTaskSnapshot = await uploadTask;
storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
imageUrl = downloadUrl;
return imageUrl;
}, onError: (err) {
return imageUrl;
//Fluttertoast.showToast(msg: 'This file is not an image');
});
} catch (e) {
return imageUrl;
}
return imageUrl;
}