1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Error uploading a photo to Firebase storage with Flutter Web using Pickers

Discussão em 'Mobile' iniciado por Stack, Novembro 6, 2024 às 13:02.

  1. Stack

    Stack Membro Participativo

    I read that Firebase storage is now compatible with Flutter web. I have this code with which I'm able to upload photos with my android version, but I keep getting this error in web. putfile() triggers the error:

    Error: Unsupported operation: Platform._operatingSystem


    My method for uploading looks like this:

    uploadImage() async {
    final _picker = ImagePicker();
    PickedFile image;
    if(kIsWeb){
    image = await _picker.getImage(source: ImageSource.gallery);
    var file = File(image.path);
    if (image != null)
    {
    var snapshot = await _storage.ref()
    .child('descriptionimages/'+widget.activity.id)
    .putFile(file);
    var downloadUrl = await snapshot.ref.getDownloadURL();
    }
    else print('No Path Received');
    }
    }


    The problem could also be linked to the image picker that I'm using. In the Readme it says: Note that on the web platform (kIsWeb == true), File is not available, so the path of the PickedFile will point to a network resource instead.

    UPDATE I installed this FilePicker and changed the code to make it like this:

    uploadImage() async {
    final _storage = FirebaseStorage.instance;
    PickedFile image;
    if(kIsWeb){
    FilePickerResult result = await FilePicker.platform.pickFiles();
    if (result != null)
    {
    File file = File(result.files.single.path);
    var snapshot = await _storage.ref()
    .child('descriptionimages/'+widget.activity.id)
    .putFile(file);
    var downloadUrl = await snapshot.ref.getDownloadURL();
    setState(()
    {
    widget.activity.image = downloadUrl;
    ActivityService().updateActivity(widget.activity);
    });
    }
    else print('No Path Received');
    }
    }


    Still, I get another error here in File file = File(result.files.single.path). The path is null and it results in an error. It seems that this plugin has the path null.

    Is there any suggestion? I'll continue my research for a Picker that supports paths in web.

    Continue reading...

Compartilhe esta Página