How to upload image in firebase storage 5.0.1 with flutter [duplicate]

喜欢而已 提交于 2021-01-29 08:00:59

问题


Recently a new version of firebase storage just came (5.0.1). There is a lot of changes. I can't seem to upload a photo and get the download URL of the photo. Can you provide a way to do it in firebase storage 5.0.1


回答1:


import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

for uploading file

Future<void> uploadFile(String filePath) async {
  File file = File(filePath);

  try {
    await firebase_storage.FirebaseStorage.instance
    .ref('uploads/file-to-upload.png')
    .putFile(file);
 } on firebase_core.FirebaseException catch (e) {
// e.g, e.code == 'canceled'
}
}

for getting download links:

Future<void> downloadURLExample() async {
 String downloadURL = await firebase_storage.FirebaseStorage.instance
  .ref('users/123/avatar.jpg')
  .getDownloadURL();

// Within your widgets:
// Image.network(downloadURL);
}

visit for depth details about this on https://firebase.flutter.dev/docs/storage/usage



来源:https://stackoverflow.com/questions/64843474/how-to-upload-image-in-firebase-storage-5-0-1-with-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!