无法使用 Flutter Web image_picker 将文件上传到 Firebase 存储

Unable to upload file to Firebase Storage using Flutter Web's image_picker

我正在使用 flutter web。我正在尝试使用 image_picker 上传图像并存储在 firebase 存储中。 image_picker returns PickedFile 类型。因此,我使用 File image = File(pickedFile.path) 将其转换为文件类型,然后使用 ref.putFile(image) 上传。但是文件没有上传。我收到命名空间异常。有什么想法吗?

PickedFile pickedFile =
          await picker.getImage(source: ImageSource.gallery);

      File newFile = File(pickedFile.path);
      var now = DateTime.now().millisecondsSinceEpoch;
      StorageReference reference =
          FirebaseStorage.instance.ref().child("images/$now");
      StorageUploadTask uploadTask = reference.putFile(newFile);
      //Upload the file to firebase

      StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;

      // Waits till the file is uploaded then stores the download url
      String url = await taskSnapshot.ref.getDownloadURL();

我得到的错误是

Error: Unsupported operation: _Namespace
    at Object.throw_ [as throw] (http://localhost:64148/dart_sdk.js:4322:11)
    at Function.get _namespace [as _namespace] (http://localhost:64148/dart_sdk.js:54027:17)
    at io._File.new.existsSync (http://localhost:64148/dart_sdk.js:51618:51)
    at firebase_storage.StorageReference.__.putFile (http://localhost:64148/packages/firebase_storage/firebase_storage.dart.lib.js:701:27)
    at add_productview._AddProductPageState.new.loadAssets (http://localhost:64148/packages/ecommerce_glasses/product/views/add_product.view.dart.lib.js:1234:38)
    at loadAssets.next (<anonymous>)
    at http://localhost:64148/dart_sdk.js:37211:33
    at _RootZone.runUnary (http://localhost:64148/dart_sdk.js:37065:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:64148/dart_sdk.js:32049:29)
    at handleValueCallback (http://localhost:64148/dart_sdk.js:32596:49)
    at Function._propagateToListeners (http://localhost:64148/dart_sdk.js:32634:17)
    at _Future.new.[_completeWithValue] (http://localhost:64148/dart_sdk.js:32477:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:64148/dart_sdk.js:32499:35)
    at Object._microtaskLoop (http://localhost:64148/dart_sdk.js:37326:13)
    at _startMicrotaskLoop (http://localhost:64148/dart_sdk.js:37332:13)
    at http://localhost:64148/dart_sdk.js:32851:9

在 index.html

中添加此脚本
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js"></script>    
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-firestore.js"></script>

我就是这样做的

// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase/firebase.dart' as fb;

Future<Uri> _uploadImageFile({
  @required html.File image,
  @required String imageName,
}) async {
fb.StorageReference storageRef = fb.storage().ref('category/$imageName');
fb.UploadTaskSnapshot uploadTaskSnapshot =
    await storageRef.put(image).future;

 Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
 return imageUri;
}

您不能在 Flutter web 中使用 File newFile = File(pickedFile.path);。我不知道 Firebase API 是如何工作的,但你可能不得不使用 pickedFile.readAsBytes().

您无权访问 Flutter web 中的文件路径。

您需要以字节形式上传文件,而不是尝试从路径上传文件。

您可以使用此方法获取文件 Uint8List

final fileBytes = pickedFile.readAsBytes();

而在你的存储代码中,你可以把数据代替,

reference.putData(fileBytes);

因此,您的代码应如下所示


PickedFile pickedFile =
          await picker.getImage(source: ImageSource.gallery);

final fileBytes = pickedFile.readAsBytes();
var now = DateTime.now().millisecondsSinceEpoch;
StorageReference reference =
  FirebaseStorage.instance.ref().child("images/$now");

StorageUploadTask uploadTask = reference.putData(fileBytes);

//Upload the file to firebase
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;

// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();