在 Flutter Web 上使用 firebase_storage 4.0.0 将图像上传到 Firebase 存储?

Uploading an Image to Firebase Storage with firebase_storage 4.0.0 on Flutter Web?

看起来像最新版本的 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});
      });
    });
  }

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

更新 - 14/04/2021 - 使用 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 which is a dependency of 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;
}