Flutter Web: firebase_storage: MissingPluginException Nessuna implementazione trovata per il metodo StorageReference # putData

Aug 27 2020

Sto cercando di caricare un'immagine nel formato UInt8List nell'archivio Firebase. Sto usando "StorageReference.putData". Continuo a ricevere questo errore che dice che putFile non è definito. Ho aggiornato a tutti i pacchetti Firebase in ritardo e ancora nessun successo. PutData non è possibile su Flutter Web?

Ho provato ad aggiornare tutti i pacchetti e poi 'flutter clean', 'flutter packages get'

Il motivo per cui non sto usando "putFile" è perché Flutter Web non supporta dart: io che contiene la classe File specifica richiesta per usare putFile.

L'errore:

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

La mia funzione di caricamento:

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

Risposte

1 broken.eggshell Sep 04 2020 at 07:13

Alla fine sono riuscito a farlo funzionare dopo aver ibridato le risposte di molte altre persone. Comincio con un'immagine da image_picker_for_web:

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

Poi:

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

Questo è stato il SO che mi ha indirizzato nella giusta direzione: Flutter Web Upload su Firestore