Flutter Web Upload to Firestore

后端 未结 1 1008
猫巷女王i
猫巷女王i 2021-01-17 23:32

I am having issues with Flutter web and uploading Images to Firestore. I\'m pretty sure the issue lies in the Image Picker, as the normal(mobile) image picker does not work

相关标签:
1条回答
  • 2021-01-18 00:03

    I have not tried the alternatives you mentioned, but below has worked for me before on Flutter web and Firebase. The event listener for uploadInput works for most platforms. The last part regarding document.body.append will ensure that it works on Mobile safari as well.

      Future<void> _setImage() async {
        final completer = Completer<String>();
        InputElement uploadInput = FileUploadInputElement();
        uploadInput.multiple = false;
        uploadInput.accept = 'image/*';
        uploadInput.click();
        
        uploadInput.addEventListener('change', (e) async {
          // read file content as dataURL
          final files = uploadInput.files;
          Iterable<Future<String>> resultsFutures = files.map((file) {
            final reader = FileReader();
            reader.readAsDataUrl(file);
            reader.onError.listen((error) => completer.completeError(error));
            return reader.onLoad.first.then((_) => reader.result as String);
          });
    
          final results = await Future.wait(resultsFutures);
          completer.complete(results[0]);
        });
    
        
        document.body.append(uploadInput);
        final String image = await completer.future;
    
        widget.newImage = uploadInput.files[0];
    
        // Upload to Firebase
        uploadToFirebase(widget.newImage); // This is dart:html File
    
        uploadInput.remove();
      }
    

    Then the upload to Firebase Storage:

    uploadToFirebase(String imageName, File file) async {
     Firebase.UploadTask task = storage.refFromURL('gs://.../images/' + imageName).put(file); 
    }
    
    0 讨论(0)
提交回复
热议问题