flutter_echarts icon indicating copy to clipboard operation
flutter_echarts copied to clipboard

setState() called after dispose()

Open rakakhrl opened this issue 5 years ago • 0 comments

So i got this error when running my app with debug (f5).

setState() called after dispose(): _EchartsState#48e6f(lifecycle state: defunct, not mounted) This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

This happen after i wrap my custom widget container the chart inside Future builder.

My screen file

            FutureBuilder<ChartDataList>(
                future: _summaryDataProvider
                    .getRegistrationTypeSummary(),
                builder: (context, snapshot) {
                  var res = snapshot.data;

                  return PieChartCard(
                    isLoading:
                        snapshot.connectionState == ConnectionState.waiting
                            ? true
                            : false,
                    title: res != null ? res.title : 'Summary Data',
                    data: res != null ? res.data : [],
                  );
            }),

My PieChartCard widget

class PieChartCard extends StatelessWidget {
  final String title;
  final List<ChartDataItem> data;
  final bool isLoading;

  PieChartCard({
    this.title,
    this.data,
    this.isLoading = true,
  }) : assert(data != null);

  List<Map<String, Object>> serializeData() {
    return data.map((d) => {'name': d.name, 'value': d.value}).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      child: _PieChart(
        isLoading: isLoading,
        title: title,
        data: serializeData(),
      ),
    );
  }
}

class _PieChart extends StatelessWidget {
  final bool isLoading;
  final String title;
  final List<Map<String, Object>> data;

  _PieChart({this.isLoading, this.title, this.data});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 280.0,
      padding: EdgeInsets.all(12.0),
      child: isLoading
          ? Center(
              child: CircularProgressIndicator(),
            )
          : Echarts(
              option: '''
          {
              title: { text: '$title' },
              tooltip: {
                  trigger: 'item',
                  formatter: '{b} : {c} ({d}%)'
              },
              legend: {
                show: true,
                orient: 'horizontal',
                top: 'bottom',
              },
              series: [
                {
                    type: 'pie',
                    radius: '50%',
                    center: ['50%', '50%'],
                    data: ${jsonEncode(data)},
                    animation: false,
                    label: {
                        show: false,
                        position: 'outer',
                        alignTo: 'none',
                    },
                }
              ]
          }
      ''',
            ),
    );
  }
}

rakakhrl avatar Aug 11 '20 03:08 rakakhrl