vendredi 5 novembre 2021

I wonder how to upload to firebase storage an image from the flutter web

I'm going to bring the image to the web and upload it to storage. I am using sdk 2.7.0 and image_picker_web: ^1.0.9. And get the image using the following code.

Below are buttons for outputting images on the screen and buttons for uploading images to storage.

  RaisedButton(
    onPressed: () {
      pickImage().then((value) {
        setState(() {
          mediaInfo = value;
        });
      });
    },
    child: Text("Select Image"),
  ),

  TextButton(
      onPressed: () async {
        StorageReference storageReference = FirebaseStorage.instance
            .ref()
            .child('testImg/post.jpg');
        StorageUploadTask uploadTask = storageReference.putFile(File(mediaInfo.data));
        await uploadTask.onComplete;
        print('File Uploaded');
        String returnURL;
        await storageReference.getDownloadURL().then((fileURL) {
          returnURL =  fileURL;
        });
        return returnURL;
      },
      child: Text("upload"))

image pick method

  Future<MediaInfo> pickImage() async {
    MediaInfo mediaInfo = await ImagePickerWeb.getImageInfo;
    // imageFile = await ImagePickerWeb.getImage(outputType: ImageType.file);
    // imageFile2 = await ImagePickerWeb.getImage(outputType: ImageType.bytes);

    if (imageFile != null) {
      print(imageFile.name);
      // print(imageFile2.toString());
    }

    // return mediaInfo;
  }

this Image files are not available for storage code. How do you handle this on the web?

 StorageUploadTask uploadTask = storageReference.putFile(File(mediaInfo.data)); //error!

Thank you.




Aucun commentaire:

Enregistrer un commentaire