【问题标题】:Uploading an Image to Firebase Storage with firebase_storage 4.0.0 on Flutter Web?在 Flutter Web 上使用 firebase_storage 4.0.0 将图像上传到 Firebase 存储?
【发布时间】:2020-12-20 14:16:55
【问题描述】:

看起来在最新版本的 Firebase Storage 中,.put(...) 方法已被弃用,取而代之的是 .putData(Uint8List) 和 .putFile(...),我还没有找到解决方案还适用于 Flutter Web。

我正在尝试的代码是这样的,但它没有返回任何内容或抛出任何错误。

 _startFilePicker() async {
    InputElement uploadInput = FileUploadInputElement();
    uploadInput.click();

    uploadInput.onChange.listen((e) {
      // read file content as dataURL
      final files = uploadInput.files;
      if (files.length == 1) {
        final file = files[0];
        FileReader reader = FileReader();

        reader.onLoadEnd.listen((e) async {
          setState(() {
            uploadedImage = reader.result;
          });
          await uploadImage();
        });

        reader.onError.listen((fileEvent) {});

        reader.readAsArrayBuffer(file);
      }
    });
  }

  Future uploadImage() async {
    StorageReference storageReference =
        FirebaseStorage.instance.ref().child(userID + '/userPhoto');
    try {
      StorageUploadTask uploadTask = storageReference.putData(uploadedImage);

      await uploadTask.onComplete;
    } catch (e) {
      print(e);
    }
    print('File Uploaded');
    storageReference.getDownloadURL().then((fileURL) {
      setState(() {
        _formData['photo'] = fileURL;
        updateUserData({'photo': fileURL});
      });
    });
  }

有什么我做错或更好的方法吗?

【问题讨论】:

    标签: firebase-storage flutter-web


    【解决方案1】:

    更新 - 2021 年 4 月 14 日 - 与 firebase_core: ^1.0.2firebase_storage: ^8.0.3 合作

    import 'package:firebase_storage/firebase_storage.dart';
    import 'package:path/path.dart';
    import 'dart:io';
    
    Future uploadProfilePhotoToFirebase(File _image) async {
      String fileName = basename(_image.path);  //Get File Name - Or set one
      Reference firebaseStorageRef = FirebaseStorage.instance.ref().child('uploads/$fileName');
      TaskSnapshot uploadTask = await firebaseStorageRef.putFile(_image);
      String url = await uploadTask.ref.getDownloadURL(); //Get URL
      return await membersCollection.doc(uid).update({ //Update url in Firestore (if required)
        'displayPhoto': url,
      });
    }
    

    老答案

    尝试使用 firebase 包 - 这适用于 firebase 7.3.0,它是 firebase_core 0.5.0 的依赖项

    import 'dart:async';
    import 'package:firebase/firebase.dart' as fb;
    import 'dart:html' as html;
    
    String url;
    
    Future<String> uploadProfilePhoto(html.File image, {String imageName}) async {
      try {
        //Upload Profile Photo
        fb.StorageReference _storage = fb.storage().ref('displayPhotos/$imageName');
        fb.UploadTaskSnapshot uploadTaskSnapshot = await _storage.put(image).future;
        // Wait until the file is uploaded then store the download url
        var imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
        url = imageUri.toString();
      } catch (e) {
        print(e);
      }
      return url;
    }
    

    【讨论】:

    • 啊,我的错!我不知道有这个包,还有firebase_storage。非常感谢,这是我很长时间以来最头疼的问题
    • 这是代码在新版本中不起作用。
    • @AbdullahKhan 你用过更新版吗?
    • @JacksonLee 是的,我也让它工作。您的代码的问题是,在网络上 putFile 不起作用。你必须处理它,而且我认为 dart:io 在网络上也不起作用。这是一个示例的链接:pub.dev/packages/firebase_storage/example
    猜你喜欢
    • 2021-08-11
    • 2021-07-28
    • 2020-04-30
    • 2018-10-24
    • 2019-01-22
    • 2021-07-03
    • 2021-03-29
    • 2022-07-28
    相关资源
    最近更新 更多