Image does not update after upload

Asked

Viewed 88 times

1

I am making an application using Flutter, but the image does not update on the screen, even after it has been saved correctly. See the steps I follow in programming.

Step 1

I enter the page and it loads the screen data:

    bool _subindoImagem = false;
    @override
    Widget build(BuildContext context) {
    
      _atualizaValoresIniciais(widget.redeModel);
    
      return Scaffold(...
    _subindoImagem
        ? CircularProgressIndicator()
        : FutureBuilder<RedeModel>(
      future: _atualizaImagem(widget.redeModel.id),
      builder: (context, snapshot) {
        ...

Step 2

I click on an image to select an image in the gallery:

    onTap: () {
      _showModalAtualizaImagem(context, "Imagem", "Buscar imagem de qual origem?", widget.redeModel.id);
    },

    A imagem é selecionada e no final do método "_showModalAtualizaImagem" faço o upload da imagem. Tudo funciona bem, pois a imagem gif do circular aparece enquanto se faz o upload e some ao finalizar.

    _imagem = _imagemSelecionada;
    if (_imagem != null) {
      setState(() {
        _subindoImagem = true;
      });
      _uploadImagem(idRede);
    }

Step 3

When finishing the upload and save the image correctly, within the "_uploadImage" method I try to update the saved image, and at this point the image is not updated.

    Future<List<RedeModel>> _uploadImagem(int idRede) async {
      final prefs = await SharedPreferences.getInstance();
      String token = await prefs.getString(ConstantesConfig.PREFERENCES_TOKEN);
      var _url = "${ConstantesRest.URL_REDE}/${idRede}/imagem";
      var request = MultipartRequest();
    
      request.setUrl(_url);
      request.addFile("file", _imagem.path);
      request.addHeaders({
        //'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': token,
      });
    
      Response response = request.send();
      try {
        print(response);
      } on Exception catch (exception) {
        print(exception);
      } catch (error) {
        print(error);
      }
    
      response.onError = () {
        setState(() {
          _subindoImagem = false;
        });
      };
    
      response.onComplete = (response) {
        //_atualizaImagem(widget.redeModel.id);
        print("Buscar imagem via http");
        setState(() {
          _subindoImagem = false;
        });
      };
    
      response.progress.listen((int progress) {
        print("Buscar imagem via http");
        /*setState(() {
          _subindoImagem = true;
        });*/
      });
    }

When modifying the variable "_subindoImage" to false, the method "_updateImage" shown in the first block of code (above) is again called to refresh the image on the page.

I also tried to call the "_updateImage" method inside the Response.onComplete, but it did not generate any effect on updating the image on screen.

The image was saved correctly on the server with the same name, that is, replacing the existing image. However, the app insists on showing the old image. I need to close the app and log in again, so it can update the new image on the page.

I appreciate if you can help, because I’ve been in this for a long time, but everything I tried didn’t get results so that the image can be displayed correctly (updated). Thank you.

  • What the _update function does?

  • Search information by running an http via Rest. _atualizaImagem(int idRede) async {&#xA; RedeService redeService = RedeService();&#xA; return redeService.buscaRedePorId(idRede, false); &#xA;}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.