Skip to content

Null check missing in code: Line chart with hidden legends throws exception #2208

Closed
@PT10

Description

Bug description

There is a clear issue in the code. When "isVisibleInLegend" is false, null is returned. And then there is no null check in the caller.

chart_series.dart

  @override
  List<LegendItem>? buildLegendItems(int index) {
    final List<LegendItem>? items = super.buildLegendItems(index);
    if (trendlineContainer != null) {
      items!.addAll(trendlineContainer!.buildLegendItems(index, this)!);
    }
    return items;
  }

Here a null check is required on items before calling items!.addAll.

Following is the method from base class which this code calls (super.buildLegendItems) and it can return null.

  @override
  List<LegendItem>? buildLegendItems(int index) {
    if (isVisibleInLegend) {
      final LegendItem legendItem = ChartLegendItem(
        text: legendItemText ?? name,
        iconType: toLegendShapeMarkerType(legendIconType, this),
        iconColor: legendIconColor(),
        iconBorderColor: legendIconBorderColor(),
        iconBorderWidth: legendIconBorderWidth(),
        series: this,
        seriesIndex: index,
        pointIndex: 0,
        isToggled: _isToggled(),
        shader: legendIconShader(),
        overlayMarkerType: markerSettings.isVisible
            ? toShapeMarkerType(markerSettings.shape)
            : null,
        imageProvider: legendIconType == LegendIconType.image
            ? parent?.legend?.image
            : null,
        onTap: handleLegendItemTapped,
        onRender: _handleLegendItemCreated,
      );
      return <LegendItem>[legendItem];
    } else {
      return null;
    }
  }

Steps to reproduce

Any line chart which a single series and show legend flag on.

Code sample

Code sample
chart_series.dart

@OverRide
List? buildLegendItems(int index) {
final List? items = super.buildLegendItems(index);
if (trendlineContainer != null) {
items!.addAll(trendlineContainer!.buildLegendItems(index, this)!);
}
return items;
}

Here a null check is required on items before calling items!.addAll.


Following is the method from base class which this code calls (super.buildLegendItems) and it can return null.

@OverRide
List? buildLegendItems(int index) {
if (isVisibleInLegend) {
final LegendItem legendItem = ChartLegendItem(
text: legendItemText ?? name,
iconType: toLegendShapeMarkerType(legendIconType, this),
iconColor: legendIconColor(),
iconBorderColor: legendIconBorderColor(),
iconBorderWidth: legendIconBorderWidth(),
series: this,
seriesIndex: index,
pointIndex: 0,
isToggled: _isToggled(),
shader: legendIconShader(),
overlayMarkerType: markerSettings.isVisible
? toShapeMarkerType(markerSettings.shape)
: null,
imageProvider: legendIconType == LegendIconType.image
? parent?.legend?.image
: null,
onTap: handleLegendItemTapped,
onRender: _handleLegendItemCreated,
);
return [legendItem];
} else {
return null;
}
}



Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Stack Traces

Stack Traces
[Add the Stack Traces here]

On which target platforms have you observed this bug?

Web

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.1, on macOS 14.3.1 23D60 darwin-arm64, locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/macos-android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.86.2)
[✓] Connected device (3 available)
[✓] Network resources

! Doctor found issues in 1 category.

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingchartsCharts componentsolvedSolved the query using existing solutions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions