Error "setState() called after Dispose()" in flutter

Asked

Viewed 289 times

0

Good morning, I’m getting the following error message on the flutter:

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: setState() called after dispose(): _RelatorioScreenState#e8cd4(lifecycle state: defunct, not mounted)

It is not happening all the time, it is random, researching saw that a solution would be to check if it is mounted before doing setState, so I did it as follows:

if (mounted) {
    setState(() {
      dadosEntrada = dados;
    });
  }

But the problem persists. I have this problem in both calls where I use Setstate. The class is like this:

class _RelatorioEntradasState extends State<RelatorioEntradas> {
  var dadosEntrada = 0.0;
  var dataChart;

  @override
  void initState() {
    super.initState();
    Intl.defaultLocale = 'pt_BR';
    _generateData();
  }

  Future<void> _generateData() async {
    try {
      final dbHelper = new RelatorioProviderDB();
      dados = await dbHelper.getDataEntradasMes();
      if (mounted) {
        setState(() {
          dadosEntrada = dados;
        });
      }
      await _dadosChart();
    } catch (error) {}
  }

  Future<void> _dadosChart() async {
    try {
      final dbHelper = new RelatorioProviderDB();
      final dadosChart = await dbHelper.getDataEntradasChart();
      dataReceita = dadosChart;

      if (mounted) {
        setState(() {
          tamanhoLista = dataReceita.length;
        });
      }

      dataReceita.forEach((e) {
        data1.add(new LancEntradas(e['descricao'], e['valor']));
      });

      _seriesData = List<charts.Series<LancEntradas, String>>();
      _seriesData.add(charts.Series(
        domainFn: (LancEntradas lancEntradas, _) => lancEntradas.descricao,
       measureFn: (LancEntradas lancEntradas, _) => lancEntradas.valor,
        id: '1',
        data: data1,
        fillPatternFn: (_, __) => charts.FillPatternType.solid,
        fillColorFn: (LancEntradas lancEntradas, _) =>
           charts.ColorUtil.fromDartColor(Color.fromARGB(255, 128, 0, 0)),
      ));
    } catch (error) {}
  }

  @override
  void dispose() {
    super.dispose();
  }
 }
  • Have you tried stopping, flutter clean and run again? Also, I think the most appropriate (in this case) would be to check the reverse (if (!mounted) return), since subsequent operations also need not be done if the widget no longer exists. There are only these two setState in its class?

  • Thanks for the answer, already tried to clean the application, initially I was doing "if (!Mounted) Return" getting the same problem. Yeah, I just got these two setState.

No answers

Browser other questions tagged

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