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

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

Open
PT10 opened this issue Dec 15, 2024 · 0 comments
Open
Labels
charts Charts component open Open

Comments

@PT10
Copy link

PT10 commented Dec 15, 2024

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.
@LavanyaGowtham2021 LavanyaGowtham2021 added charts Charts component open Open labels Dec 16, 2024
@PT10 PT10 changed the title Line chart with one series throws exception Null check missing in code: Line chart with hidden labels throws exception Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
charts Charts component open Open
Projects
None yet
Development

No branches or pull requests

2 participants