Skip to content

Errors while adding values to a StackedColumnSeries during runtime with updateDataSource #1817

@DrNiels

Description

@DrNiels

Bug description

While adding values to a StackedColumnSeries and confirming them via controller.updateDataSource, sometimes an error is thrown. I could not observe when it does and when it does not, but the following example throws them a lot. I use the version 25.1.40 of syncfusion_flutter_charts.

Steps to reproduce

Load the code sample and pan to the left.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

void main() async {
  runApp(
    MaterialApp(
      home: TestChart(),
    ),
  );
}

class TestChart extends StatefulWidget {
  _TestChartState createState() => _TestChartState();
}

class _TestChartState extends State<TestChart> {
  // Just ensure that the list is not initially empty
  List<Offset> currentData = [
    Offset(DateTime(2024, 4, 3).millisecondsSinceEpoch.toDouble(), 5),
  ];
  ChartSeriesController<Offset, DateTime>? controller;

  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
      series: [
        StackedColumnSeries<Offset, DateTime>(
          xValueMapper: (data, _) =>
              DateTime.fromMillisecondsSinceEpoch(data.dx.round()),
          yValueMapper: (data, _) => data.dy,
          dataSource: currentData,
          onRendererCreated:
              (ChartSeriesController<Offset, DateTime> controller) {
            this.controller = controller;
          },
        ),
      ],
      primaryYAxis: NumericAxis(
        minimum: 0,
        maximum: 20,
      ),
      primaryXAxis: DateTimeAxis(
        name: 'xAxis',
        minimum: DateTime(2020),
        maximum: DateTime(2025),
        initialVisibleMinimum: DateTime(2024, 4, 3),
        initialVisibleMaximum: DateTime(2024, 4, 14),
        intervalType: DateTimeIntervalType.days,
      ),
      zoomPanBehavior: ZoomPanBehavior(
        enablePanning: true,
        zoomMode: ZoomMode.x,
      ),
      onActualRangeChanged: (ActualRangeChangedArgs args) {
        if (args.axisName != 'xAxis') {
          return;
        }

        WidgetsBinding.instance.addPostFrameCallback((Duration duration) {
          setState(() {
            updateData(
              start: DateTime.fromMillisecondsSinceEpoch(args.visibleMin.round()),
              end: DateTime.fromMillisecondsSinceEpoch(args.visibleMax.round()),
            );
          });
        });
      },
    );
  }

  void updateData({
    required DateTime start,
    required DateTime end,
  }) {
    while (start.isBefore(_doubleToDateTime(this.currentData.first.dx))) {
      this.currentData.insert(
            0,
            Offset(
                _doubleToDateTime(this.currentData.first.dx)
                    .subtract(Duration(days: 1))
                    .millisecondsSinceEpoch,
                5),
          );
      controller?.updateDataSource(addedDataIndex: 0);
    }
  }

  _doubleToDateTime(double dx) {
    return DateTime.fromMillisecondsSinceEpoch(dx.round());
  }
}

Screenshots or Video

In the code sample, the error does not become visible in the UI, but it can definitely affect other examples as the callback is ended due to a thrown error.

Stack Traces

Stack Traces
══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
The following IndexError was thrown during a scheduler callback:
RangeError (index): Index out of range: no indices are valid: 0

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3       throw_
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 600:7                 _get]
packages/syncfusion_flutter_charts/src/charts/series/chart_series.dart 6129:33    [_computeStackedValues]
packages/syncfusion_flutter_charts/src/charts/series/chart_series.dart 6068:7     [_calculateStackingValues]
packages/syncfusion_flutter_charts/src/charts/series/chart_series.dart 6398:5     updateDataPoints
packages/syncfusion_flutter_charts/src/charts/series/chart_series.dart 2693:15    updateDataSource
reproduce.dart 84:19                                                              updateData
reproduce.dart 61:13                                                              <fn>
packages/flutter/src/widgets/framework.dart 1203:30                               setState
reproduce.dart 60:11                                                              <fn>
packages/flutter/src/scheduler/binding.dart 1386:7                                [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1322:11                               handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1169:5                                [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1346:5                                invoke
lib/_engine/engine/platform_dispatcher.dart 260:5                                 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 185:36                                     <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 550:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 555:39  dcall
════════════════════════════════════════════════════════════════════════════════════════════════════

On which target platforms have you observed this bug?

Web

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.19.5, on Microsoft Windows [Version 10.0.19045.4291], locale de-DE)
    • Flutter version 3.19.5 on channel stable at C:\Users\Niels\Documents\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (3 weeks ago), 2024-03-27 21:54:07 -0500      
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc4)
    • Android SDK at C:\Users\Niels\AppData\Local\Android\Sdk
    • Platform android-34, build-tools 34.0.0-rc4
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java       
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Users\Niels\AppData\Local\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.6)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.8.34525.116
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[√] VS Code, 64-bit edition (version 1.87.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.86.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.4291]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 123.0.6312.123
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 123.0.2420.97

[√] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingchartsCharts componentfixedFixed and delivered updatewaiting for customer responseCannot make further progress until the customer responds.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions