Flutter Web: firebase_storage: MissingPluginException No implementation found for method StorageReference#putData
Asked Answered
S

2

3

I am trying to upload an image in UInt8List format to firebase storage. I am using "StorageReference.putData". I keep getting this error saying that putFile isn't defined. I have updated to all the lated firebase packages and still no success. Is putData not possible on Flutter Web?

I have tried to update all packages and then 'flutter clean', 'flutter packages get'

The reason I am not using "putFile" is because Flutter Web doesn't support dart:io which holds the specific File class required for using putFile.

The Error:

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

My upload function:

  Future<StorageTaskSnapshot> uploadImage(Uint8List imageFile, int pos) {
    return storageRef
        .child("posts/${currentUser.uid}/$_postId/$pos.jpg")
        .putData(imageFile)
        .onComplete;
  }
Sukey answered 26/8, 2020 at 19:58 Comment(3)
It appears the cloud_firestore plugin doesn't support flutter web yet. I will search for alternative. Possibly the firebase_dart plugin will work.Sukey
Did you have any luck with this? I tried putData and also tried putFile using the in-memory filesystem provided by the file package, but neither one works.Syndetic
Yes. My solution was a bit of a mess. Check below for a clean answer, same idea.Sukey
S
4

I finally got this to work after hybridizing several other people's answers. I start with an image from image_picker_for_web:

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

Then:

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);
}

This was the SO that pointed me in the right direction: Flutter Web Upload to Firestore

Syndetic answered 4/9, 2020 at 0:13 Comment(2)
Thank you. I also ended up making a frankenstein solution from clues I found scattered around flutter web. Looking forward to when Flutter Web is more developed and out of betaSukey
Completely agree. I am hoping that it will turn out to be a nice PWA framework. I really don't want to have to learn Angular.Syndetic
J
2

flutter clean

flutter pub get

Then run app again.

Jadeite answered 5/1, 2023 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.