Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Exception thrown when switching from last to first chart within the #249

Open
usrdjan opened this issue Apr 25, 2019 · 2 comments
Open

Exception thrown when switching from last to first chart within the #249

usrdjan opened this issue Apr 25, 2019 · 2 comments

Comments

@usrdjan
Copy link

usrdjan commented Apr 25, 2019

I/flutter ( 912): ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 912): The following assertion was thrown during a scheduler callback:
I/flutter ( 912): setState() called after dispose(): BaseChartState#d9d5b(lifecycle state: defunct, not
I/flutter ( 912): mounted, tickers: tracking 0 tickers)
I/flutter ( 912): This error happens if you call setState() on a State object for a widget that no longer appears in
I/flutter ( 912): the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error
I/flutter ( 912): can occur when code calls setState() from a timer or an animation callback. The preferred solution
I/flutter ( 912): is to cancel the timer or stop listening to the animation in the dispose() callback. Another
I/flutter ( 912): solution is to check the "mounted" property of this object before calling setState() to ensure the
I/flutter ( 912): object is still in the tree.
I/flutter ( 912): This error might indicate a memory leak if setState() is being called because another object is
I/flutter ( 912): retaining a reference to this State object after it has been removed from the tree. To avoid memory
I/flutter ( 912): leaks, consider breaking the reference to this object during dispose().
I/flutter ( 912):
I/flutter ( 912): When the exception was thrown, this was the stack:
I/flutter ( 912): #0 State.setState. (package:flutter/src/widgets/framework.dart:1095:9)
I/flutter ( 912): #1 State.setState (package:flutter/src/widgets/framework.dart:1121:6)
I/flutter ( 912): #2 BaseChartState.requestRebuild (package:charts_flutter/src/base_chart_state.dart:71:5)
I/flutter ( 912): #3 ChartContainerRenderObject.requestRebuild.doRebuild (package:charts_flutter/src/chart_container.dart:268:19)
I/flutter ( 912): #4 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
I/flutter ( 912): #5 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:936:9)
I/flutter ( 912): #6 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:840:5)
I/flutter ( 912): #10 _invoke (dart:ui/hooks.dart:209:10)
I/flutter ( 912): #11 _drawFrame (dart:ui/hooks.dart:168:3)
I/flutter ( 912): (elided 3 frames from package dart:async)
I/flutter ( 912): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 912): Another exception was thrown: setState() called after dispose(): BaseChartState#73a56(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers)
I/flutter ( 912): Another exception was thrown: setState() called after dispose(): BaseChartState#e7d27(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers)

// CODE:

import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:main/const.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class ScreenHomeAnimatedCharts extends StatefulWidget {
@OverRide
_ScreenHomeAnimatedChartsState createState() =>
_ScreenHomeAnimatedChartsState();
}

class _ScreenHomeAnimatedChartsState extends State {
List<charts.Series<Pollution, String>> seriesBarData;
List<charts.Series<Task, String>> seriesPieData;
List<charts.Series<Sales, int>> seriesLineData;

@OverRide
void initState() {
super.initState();
seriesBarData = List<charts.Series<Pollution, String>>();
seriesPieData = List<charts.Series<Task, String>>();
seriesLineData = List<charts.Series<Sales, int>>();
generateData();
}

generateData() {
List barData1 = [
Pollution(year: 1980, place: 'USA', quantity: 20),
Pollution(year: 1980, place: 'Asia', quantity: 40),
Pollution(year: 1980, place: 'Europe', quantity: 10),
];
List barData2 = [
Pollution(year: 1980, place: 'USA', quantity: 50),
Pollution(year: 1980, place: 'Asia', quantity: 30),
Pollution(year: 1980, place: 'Europe', quantity: 40),
];
List barData3 = [
Pollution(year: 1980, place: 'USA', quantity: 70),
Pollution(year: 1980, place: 'Asia', quantity: 70),
Pollution(year: 1980, place: 'Europe', quantity: 100),
];

List<Task> pieData = [
  Task(task: 'Work', taskValue: 35.0, colorValue: Colors.red),
  Task(task: 'Sleep', taskValue: 35.0, colorValue: Colors.green),
  Task(task: 'Rest', taskValue: 30.0, colorValue: Colors.blue),
];

List<Sales> linesData1 = [
  Sales(year: 0, value: 23),
  Sales(year: 1, value: 28),
  Sales(year: 2, value: 26),
  Sales(year: 3, value: 24),
  Sales(year: 4, value: 29),
];
List<Sales> linesData2 = [
  Sales(year: 0, value: 31),
  Sales(year: 1, value: 32),
  Sales(year: 2, value: 35),
  Sales(year: 3, value: 38),
  Sales(year: 4, value: 39),
];

/* BAR CHART DATA RED SECTION*/
seriesBarData.add(
  charts.Series(
    id: '2017',
    data: barData1,
    domainFn: (Pollution pollution, _) => pollution.place,
    measureFn: (Pollution pollution, _) => pollution.quantity,
    fillPatternFn: (_, __) => charts.FillPatternType.solid,
    fillColorFn: (Pollution pollution, _) =>
        charts.ColorUtil.fromDartColor(Colors.red),
  ),
);
/* BAR CHART DATA GREEN SECTION*/
seriesBarData.add(
  charts.Series(
    id: '2018',
    data: barData2,
    domainFn: (Pollution pollution, _) => pollution.place,
    measureFn: (Pollution pollution, _) => pollution.quantity,
    fillPatternFn: (_, __) => charts.FillPatternType.solid,
    fillColorFn: (Pollution pollution, _) =>
        charts.ColorUtil.fromDartColor(Colors.green),
  ),
);
/* BAR CHART DATA BLUE SECTION*/
seriesBarData.add(
  charts.Series(
    id: '2019',
    data: barData3,
    domainFn: (Pollution pollution, _) => pollution.place,
    measureFn: (Pollution pollution, _) => pollution.quantity,
    fillPatternFn: (_, __) => charts.FillPatternType.forwardHatch,
    fillColorFn: (Pollution pollution, _) =>
        charts.ColorUtil.fromDartColor(Colors.blue),
  ),
);

/* PIE CHART DATA */
seriesPieData.add(
  charts.Series(
    id: 'Task',
    data: pieData,
    domainFn: (Task task, _) => task.task,
    measureFn: (Task task, _) => task.taskValue,
    colorFn: (Task task, _) =>
        charts.ColorUtil.fromDartColor(task.colorValue),
    labelAccessorFn: (Task task, _) => '${task.taskValue}',
  ),
);

/* LINE CHART DATA RED */
seriesLineData.add(charts.Series(
  id: 'Sales',
  data: linesData1,
  domainFn: (Sales sales, _) => sales.year,
  measureFn: (Sales sales, _) => sales.value,
  colorFn: (Sales sales, _) => charts.ColorUtil.fromDartColor(Colors.red),
));
/* LINE CHART DATA BLUE */
seriesLineData.add(charts.Series(
  id: 'Sales',
  data: linesData2,
  domainFn: (Sales sales, _) => sales.year,
  measureFn: (Sales sales, _) => sales.value,
  colorFn: (Sales sales, _) => charts.ColorUtil.fromDartColor(Colors.blue),
));

}

@OverRide
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Const.colorMain,
title: Text('Charts'),
centerTitle: true,
elevation: 0,
bottom: TabBar(
indicatorColor: Colors.white,
tabs: [
Tab(icon: Icon(FontAwesomeIcons.solidChartBar)),
Tab(icon: Icon(FontAwesomeIcons.chartPie)),
Tab(icon: Icon(FontAwesomeIcons.chartLine)),
],
),
),
body: TabBarView(
children: [
/* BAR CHART */
Container(
child: Center(
child: Column(
children: [
Container(
alignment: Alignment.bottomLeft,
padding: EdgeInsets.all(20),
child: Text(
'Bar Chart\nPollution by world region',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(10),
child: charts.BarChart(
seriesBarData,
// barGroupingType: charts.BarGroupingType.stacked,
// barGroupingType: charts.BarGroupingType.grouped,
animate: true,
animationDuration: Duration(seconds: 1),
),
),
)
],
),
),
),

        /* PIE CHART */
        Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(20),
                  alignment: Alignment.bottomLeft,
                  child: Text(
                    'Pie Chart\nTime spent on daily tasks',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.all(10),
                    child: charts.PieChart(
                      seriesPieData,
                      animate: true,
                      animationDuration: Duration(seconds: 1),
                      defaultRenderer: charts.ArcRendererConfig(
                        arcWidth: 100,
                        arcRendererDecorators: [
                          charts.ArcLabelDecorator(
                              labelPosition: charts.ArcLabelPosition.inside)
                        ],
                      ),
                      behaviors: [
                        charts.DatumLegend(
                          outsideJustification:
                              charts.OutsideJustification.endDrawArea,
                          horizontalFirst: false,
                          desiredMaxRows: 2,
                          cellPadding: EdgeInsets.only(right: 4, bottom: 4),
                          entryTextStyle: charts.TextStyleSpec(
                              color: charts.MaterialPalette.gray.shade800,
                              fontFamily: 'Roboto',
                              fontSize: 15),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ),

        /* LINE CHART */
        Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(20),
                  alignment: Alignment.bottomLeft,
                  child: Text(
                    'Line Chart\nBLA BLA',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Expanded(
                  child: charts.LineChart(
                    seriesLineData,
                    animate: true,
                    animationDuration: Duration(seconds: 1),
                  ),
                )
              ],
            ),
          ),
        )
      ],
    ),
  ),
);

}
}

class Task {
String task;
double taskValue;
Color colorValue;

Task({this.task, this.taskValue, this.colorValue});
}

class Pollution {
String place;
int year, quantity;

Pollution({this.place, this.year, this.quantity});
}

class Sales {
int year;
int value;

Sales({this.year, this.value});
}

@TJ-C
Copy link

TJ-C commented May 13, 2019

I have a similar issue with grouped bar charts plotted within a TabBarView, commenting out behaviors prevented the exception errors, but now I have no legend.

Say you have four tabs, if you jump a tab, i.e. from the first tab to the third tab, an exception is shown, but not if you sequential tap each tab, i.e. first tab, second tab, third tab, forth tab, third tab etc..

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale en-GB)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
✗ Verify that all connected devices have been paired with this computer in Xcode.
If all devices have been paired, libimobiledevice and ideviceinstaller may require updating.
To update with Brew, run:
brew update
brew uninstall --ignore-dependencies libimobiledevice
brew uninstall --ignore-dependencies usbmuxd
brew install --HEAD usbmuxd
brew unlink usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
[✓] Android Studio (version 3.4)
[✓] VS Code (version 1.33.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

@cuong292
Copy link

still nothing on this yet?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants