Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when animating a pie chart from state with fewer sections to one with more sections #861

Closed
ToyWalrus opened this issue Dec 20, 2021 · 3 comments
Labels
bug Something isn't working Pie Chart

Comments

@ToyWalrus
Copy link

The issue seems to be that when implicitly animating a PieChart from a state where it has X number of sections to a state where it has Y number of sections where X < Y, the following error gets thrown:

flutter: ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during a scheduler callback:
flutter: Updated layout information required for RenderLimitedBox#fe2fd NEEDS-LAYOUT NEEDS-PAINT to calculate
flutter: semantics.
flutter: 'package:flutter/src/rendering/object.dart': package:flutter/…/rendering/object.dart:1
flutter: Failed assertion: line 2653 pos 12: '!_needsLayout'

The odd thing about this bug is that it only seems to happen once per app session, i.e. it's not reproducible again unless you do an app reset not just a hot reload.

class CrashExample extends StatefulWidget {
  @override
  _CrashExampleState createState() => _CrashExampleState();
}

class _CrashExampleState extends State<CrashExample> {
  List<double> get state0 => [1, 2, 3, 4];
  List<double> get state1 => [1, 2, 3, 4, 5];
  final colors = [Colors.red, Colors.blue, Colors.purple, Colors.green, Colors.grey];

  int currentState = 0;
  List<double> get stateVals => currentState == 0 ? state0 : state1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Crash Example')),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(onPressed: switchState, child: Text('Update')),
            SizedBox(height: 20),
            SizedBox(
              key: ValueKey('ChartContainer'),
              height: 150,
              child: PieChart(
                PieChartData(
                  sections: makeSections(),
                  centerSpaceColor: Colors.transparent,
                  borderData: FlBorderData(show: false),
                  centerSpaceRadius: double.infinity,
                  startDegreeOffset: -90,
                  sectionsSpace: 1,
                ),
                swapAnimationCurve: Curves.easeOut,
                swapAnimationDuration: Duration(milliseconds: 250),
              ),
            ),
          ],
        ),
      ),
    );
  }

  List<PieChartSectionData> makeSections() {
    final sections = <PieChartSectionData>[];
    for (final val in stateVals) {
      sections.add(PieChartSectionData(
        value: val,
        title: val.toString(),
        radius: 15,
        showTitle: false,
        color: colors[val.toInt() - 1],
      ));
    }

    return sections;
  }

  void switchState() {
    setState(() {
      currentState = (currentState + 1) % 2;
    });
  }
}

Versions

  • Flutter 2.2.3
  • fl_chart ^0.36.2
@imaNNeo
Copy link
Owner

imaNNeo commented Feb 12, 2022

Hi.
We have just fixed it in 0.45.0.
Please check it out!

@imaNNeo imaNNeo closed this as completed Feb 12, 2022
@jingfeng-cheah
Copy link

jingfeng-cheah commented Oct 13, 2023

@imaNNeo Hi, unfortunately this issue still persists for me in 0.62.0. I had to set swapAnimationDuration: Duration.zero to prevent the error.

@imaNNeo
Copy link
Owner

imaNNeo commented Nov 11, 2023

@imaNNeo Hi, unfortunately this issue still persists for me in 0.62.0. I had to set swapAnimationDuration: Duration.zero to prevent the error.

Can you provide a reproducible code? a main.dart file code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Pie Chart
Projects
None yet
Development

No branches or pull requests

3 participants