【问题标题】:Flutter Web: firebase_storage: MissingPluginException No implementation found for method StorageReference#putDataFlutter Web:firebase_storage:MissingPluginException找不到方法StorageReference#putData的实现
【发布时间】:2020-12-15 16:25:56
【问题描述】:

我正在尝试将 UInt8List 格式的图像上传到 Firebase 存储。我正在使用“StorageReference.putData”。我不断收到这个错误,说 putFile 没有定义。我已经更新了所有最新的 firebase 软件包,但仍然没有成功。在 Flutter Web 上不能 putData 吗?

我尝试更新所有包,然后'flutter clean','flutter packages get'

我不使用“putFile”的原因是因为 Flutter Web 不支持 dart:io,它包含使用 putFile 所需的特定 File 类。

错误:

Error: MissingPluginException(No implementation found for method StorageReference#putData on channel plugins.flutter.io/firebase_storage)
    at Object.throw_ [as throw] (http://localhost:60357/dart_sdk.js:4331:11)
    at MethodChannel._invokeMethod (http://localhost:60357/packages/flutter/src/services/system_channels.dart.lib.js:942:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:60357/dart_sdk.js:37593:33
    at _RootZone.runUnary (http://localhost:60357/dart_sdk.js:37447:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:60357/dart_sdk.js:32424:29)
    at handleValueCallback (http://localhost:60357/dart_sdk.js:32971:49)
    at Function._propagateToListeners (http://localhost:60357/dart_sdk.js:33009:17)
    at _Future.new.[_completeWithValue] (http://localhost:60357/dart_sdk.js:32852:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:60357/dart_sdk.js:32874:35)
    at Object._microtaskLoop (http://localhost:60357/dart_sdk.js:37708:13)
    at _startMicrotaskLoop (http://localhost:60357/dart_sdk.js:37714:13)
    at http://localhost:60357/dart_sdk.js:33226:9

我的上传功能:

  Future<StorageTaskSnapshot> uploadImage(Uint8List imageFile, int pos) {
    return storageRef
        .child("posts/${currentUser.uid}/$_postId/$pos.jpg")
        .putData(imageFile)
        .onComplete;
  }

【问题讨论】:

  • cloud_firestore 插件似乎还不支持flutter web。我会寻找替代方案。 firebase_dart 插件可能会起作用。
  • 你有什么运气吗?我尝试了putData,也尝试了使用文件包提供的内存文件系统的putFile,但都没有。
  • 是的。我的解决方案有点混乱。检查下面的一个干净的答案,同样的想法。

标签: firebase-storage flutter-web


【解决方案1】:

在混合了其他几个人的答案后,我终于得到了这个工作。我从 image_picker_for_web 中的一张图片开始:

final picker = ImagePicker();
final new_image = await picker.getImage(
    source: source=='gallery' ? ImageSource.gallery : ImageSource.camera,
);

然后:

String url;
var bytes = await new_image.readAsBytes();
try {
    fb.StorageReference _storage = fb.storage().ref('002test');
    fb.UploadTaskSnapshot uploadTaskSnapshot = await _storage.put(bytes, fb.UploadMetadata(contentType: 'image/png')).future;
    var imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
    url = imageUri.toString();
} catch (e) {
    print(e);
}

这是为我指明正确方向的 SO:Flutter Web Upload to Firestore

【讨论】:

  • 谢谢。最后,我还根据我发现的散布在颤振网络上的线索制作了一个科学怪人的解决方案。期待 Flutter Web 何时更成熟并退出测试版
  • 完全同意。我希望它会成为一个不错的 PWA 框架。我真的不想学习 Angular。
猜你喜欢
  • 2019-06-26
  • 2020-03-26
  • 2020-03-26
  • 2020-09-10
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
相关资源
最近更新 更多