Flutter Charts NoSuchMethodError getter 'length' 被调用为 null

Flutter Charts NoSuchMethodError The getter 'length' was called on null

当我在我的 Flutter 应用程序中转到包含图表的页面,然后转到另一个页面,然后 return 到该页面时,我收到以下错误。然后显示错误。我找不到这个错误的任何原因。因此这是我的代码。

I/flutter ( 8555): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 8555): The following NoSuchMethodError was thrown building LayoutId-[<chartContainer>](id: chartContainer):
I/flutter ( 8555): The getter 'length' was called on null.
I/flutter ( 8555): Receiver: null
I/flutter ( 8555): Tried calling: length
I/flutter ( 8555): 
I/flutter ( 8555): The relevant error-causing widget was:
I/flutter ( 8555):   LayoutId-[<chartContainer>]
I/flutter ( 8555):   file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/charts_flutter-0.9.0/lib/src/base_chart_state.dart:116:26 
I/flutter ( 8555):
I/flutter ( 8555): When the exception was thrown, this was the stack:
I/flutter ( 8555): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter ( 8555): #1      new MutableSeries (package:charts_common/src/chart/common/processed_series.dart:96:30)
I/flutter ( 8555): #2      BaseChart.makeSeries (package:charts_common/src/chart/common/base_chart.dart:514:15)
I/flutter ( 8555): #3      CartesianChart.makeSeries (package:charts_common/src/chart/cartesian/cartesian_chart.dart:286:32)
I/flutter ( 8555): #4      MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
I/flutter ( 8555): #5      ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
I/flutter ( 8555): #6      new List.from (dart:core-patch/array_patch.dart:57:19)
I/flutter ( 8555): #7      BaseChart.draw (package:charts_common/src/chart/common/base_chart.dart:450:9)
I/flutter ( 8555): #8      ChartContainerRenderObject.reconfigure (package:charts_flutter/src/chart_container.dart:170:14)
I/flutter ( 8555): #9      ChartContainer.createRenderObject (package:charts_flutter/src/chart_container.dart:62:49)
I/flutter ( 8555): #10     RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5491:28)
I/flutter ( 8555): #11     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6117:11)
I/flutter ( 8555): ...     Normal element mounting (7 frames)
I/flutter ( 8555): #18     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
I/flutter ( 8555): #19     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6236:32)
I/flutter ( 8555): ...     Normal element mounting (15 frames)
I/flutter ( 8555): #34     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
I/flutter ( 8555): #35     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6236:32)
I/flutter ( 8555): ...     Normal element mounting (41 frames)
I/flutter ( 8555): #76     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
I/flutter ( 8555): #77     Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
I/flutter ( 8555): #78     SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:1158:36)
I/flutter ( 8555): #79     SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1143:20)
I/flutter ( 8555): #80     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2683:19)
I/flutter ( 8555): #81     SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1136:11)
I/flutter ( 8555): #82     RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:350:23)
I/flutter ( 8555): #83     RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1883:59) 
I/flutter ( 8555): #84     PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:915:15)
I/flutter ( 8555): #85     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1883:14)
I/flutter ( 8555): #86     RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:339:5)
I/flutter ( 8555): #87     RenderSliverMultiBoxAdaptor.insertAndLayoutChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:485:5)
I/flutter ( 8555): #88     RenderSliverList.performLayout.advance (package:flutter/src/rendering/sliver_list.dart:231:19)
I/flutter ( 8555): #89     RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:274:19)
I/flutter ( 8555): #90     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #91     RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:132:12)    
I/flutter ( 8555): #92     RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:371:11)
I/flutter ( 8555): #93     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #94     RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:507:13)
I/flutter ( 8555): #95     RenderShrinkWrappingViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1904:12)
I/flutter ( 8555): #96     RenderShrinkWrappingViewport.performLayout (package:flutter/src/rendering/viewport.dart:1862:20)
I/flutter ( 8555): #97     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #98     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #99     RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #100    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #101    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #102    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #103    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #104    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #105    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #106    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #107    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #108    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #109    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #110    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #111    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #112    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #113    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #114    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #115    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #116    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #117    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #118    RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
I/flutter ( 8555): #119    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #120    RenderSliverMultiBoxAdaptor.insertAndLayoutLeadingChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:457:19)
I/flutter ( 8555): #121    RenderSliverFixedExtentBoxAdaptor.performLayout (package:flutter/src/rendering/sliver_fixed_extent_list.dart:234:32)
I/flutter ( 8555): #122    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #123    RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:132:12)    
I/flutter ( 8555): #124    _RenderSliverFractionalPadding.performLayout (package:flutter/src/widgets/sliver_fill.dart:170:11)        
I/flutter ( 8555): #125    RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)
I/flutter ( 8555): #126    RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:507:13)
I/flutter ( 8555): #127    RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1561:12)
I/flutter ( 8555): #128    RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1470:20)
I/flutter ( 8555): #129    RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 8555): #130    PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:884:18)
I/flutter ( 8555): #131    RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:436:19)
I/flutter ( 8555): #132    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:914:13)
I/flutter ( 8555): #133    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:302:5)
I/flutter ( 8555): #134    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
I/flutter ( 8555): #135    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1055:9)
I/flutter ( 8555): #136    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:971:5)
I/flutter ( 8555): (elided 3 frames from dart:async)
I/flutter ( 8555):
I/flutter ( 8555): ════════════════════════════════════════════════════════════════════════════════════════════════════

这是我的代码:

import 'dart:convert';

import 'package:Trimlog/app_localizations.dart';
import 'package:Trimlog/models/boat.dart';
import 'package:Trimlog/models/trim.dart';
import 'package:flutter/material.dart';
import 'package:global_configuration/global_configuration.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;
import 'package:charts_flutter/flutter.dart' as charts;

Map<String, dynamic> trimOptions = {};
Map<String, dynamic> trimOption = {};

class AnalyticsGraphs extends StatefulWidget {
  final Boat boat;

  AnalyticsGraphs(this.boat);

  @override
  _AnalyticsGraphsState createState() => _AnalyticsGraphsState();
}

class _AnalyticsGraphsState extends State<AnalyticsGraphs> {
  bool loading = true; // Enable loading

  Future getBoatClassTrimTemplate() async {
    String boatClassJson = (await http.get(GlobalConfiguration().getValue("static_api")["boatClasses"] + "/" + widget.boat.boatClass)).body;
    trimOptions = await json.decode(boatClassJson)["trimTemplate"];
  }

  @override
  void initState() {
    super.initState();
    getBoatClassTrimTemplate().then((value) {
      setState(() {
        loading = false;
      });
    });
  } // Execute once on form open

  @override
  Widget build(BuildContext context) {
    final trims = Provider.of<List<Trim>>(context) ?? null;
    List<charts.Series<dynamic, num>> series = [
      charts.Series(
        measureFn: (dynamic trim, _) => trim.weather["windSpeed"],
        domainFn: (dynamic trim, _) => trim.trim[trimOption.keys.first],
        colorFn: (dynamic trim, _) {
          if (trim.satisfaction < 20) {
            return charts.ColorUtil.fromDartColor(Colors.red);
          } else if (trim.satisfaction < 40) {
            return charts.ColorUtil.fromDartColor(Colors.deepOrange);
          } else if (trim.satisfaction < 60) {
            return charts.ColorUtil.fromDartColor(Colors.orange);
          } else if (trim.satisfaction < 80) {
            return charts.ColorUtil.fromDartColor(Colors.lightGreen);
          } else if (trim.satisfaction <= 100) {
            return charts.ColorUtil.fromDartColor(Colors.green);
          }
          return charts.ColorUtil.fromDartColor(Colors.grey);
        },
        id: "windSpeed",
        data: trims,
      ),
    ];

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
          child: DropdownButton<String>(
            isExpanded: true,
            hint: Text(AppLocalizations.of(context).translate("boats.trimOption")),
            elevation: 16,
            style: Theme.of(context).textTheme.bodyText1,
            value: trimOption.isNotEmpty ? trimOption.keys.first : null,
            onChanged: (String val) {
              setState(() {
                trimOption.clear();
                trimOption[val] = trimOptions[val];
              });
            },
            items: trimOptions
                .map((key, value) {
                  return MapEntry(
                    key,
                    DropdownMenuItem<String>(
                      value: key,
                      child: Text(AppLocalizations.of(context).translate(value["labels"]["label"])),
                    ),
                  );
                })
                .values
                .toList(),
          ),
        ),
        trimOption.isNotEmpty
            ? Padding(
                padding: const EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
                child: SizedBox(
                  height: 400.0,
                  child: charts.LineChart(
                    series,
                    behaviors: [
                      new charts.ChartTitle(AppLocalizations.of(context).translate(trimOption.values.first["labels"]["label"]),
                          behaviorPosition: charts.BehaviorPosition.bottom, titleStyleSpec: charts.TextStyleSpec(fontSize: 11), titleOutsideJustification: charts.OutsideJustification.middleDrawArea),
                      new charts.ChartTitle(AppLocalizations.of(context).translate("weather.windSpeed") + " (" + AppLocalizations.of(context).translate("units.knots") + ")",
                          behaviorPosition: charts.BehaviorPosition.start, titleStyleSpec: charts.TextStyleSpec(fontSize: 11), titleOutsideJustification: charts.OutsideJustification.middleDrawArea)
                    ],
                    defaultRenderer: new charts.LineRendererConfig(
                      includeLine: true,
                      includePoints: true,
                    ),
                    animate: false,
                    animationDuration: Duration(seconds: 0),
                    defaultInteractions: false,
                  ),
                ),
              )
            : Container(),
      ],
    );
  }
}

如果我随后切换到另一个页面,我会收到下一个错误,这会使应用程序完全停止工作。

I/flutter ( 8555): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 3427 pos 12: 'child._parent == this': is not true.
I/flutter ( 8555): Another exception was thrown: A RenderViewport expected a child of type RenderSliver but received a child of type RenderErrorBox.
I/flutter ( 8555): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4345 pos 14: 'owner._debugCurrentBuildTarget == this': is not true.

您的修剪列表为空。所以你不能创建空数据的图表。你也需要检查一下。您的构建方法将如下所示:


  @override
  Widget build(BuildContext context) {
    final trims = Provider.of<List<Trim>>(context) ?? null;
    List<charts.Series<dynamic, num>> series = [];

    // Here is important!
    if (trims != null) {
      series = [
        charts.Series(
          measureFn: (dynamic trim, _) => trim.weather["windSpeed"],
          domainFn: (dynamic trim, _) => trim.trim[trimOption.keys.first],
          colorFn: (dynamic trim, _) {
            if (trim.satisfaction < 20) {
              return charts.ColorUtil.fromDartColor(Colors.red);
            } else if (trim.satisfaction < 40) {
              return charts.ColorUtil.fromDartColor(Colors.deepOrange);
            } else if (trim.satisfaction < 60) {
              return charts.ColorUtil.fromDartColor(Colors.orange);
            } else if (trim.satisfaction < 80) {
              return charts.ColorUtil.fromDartColor(Colors.lightGreen);
            } else if (trim.satisfaction <= 100) {
              return charts.ColorUtil.fromDartColor(Colors.green);
            }
            return charts.ColorUtil.fromDartColor(Colors.grey);
          },
          id: "windSpeed",
          data: trims,
        ),
      ];
    }

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
          child: DropdownButton<String>(
            isExpanded: true,
            hint: Text(
                AppLocalizations.of(context).translate("boats.trimOption")),
            elevation: 16,
            style: Theme.of(context).textTheme.bodyText1,
            value: trimOption.isNotEmpty ? trimOption.keys.first : null,
            onChanged: (String val) {
              setState(() {
                trimOption.clear();
                trimOption[val] = trimOptions[val];
              });
            },
            items: trimOptions
                .map((key, value) {
                  return MapEntry(
                    key,
                    DropdownMenuItem<String>(
                      value: key,
                      child: Text(AppLocalizations.of(context)
                          .translate(value["labels"]["label"])),
                    ),
                  );
                })
                .values
                .toList(),
          ),
        ),
        trimOption.isNotEmpty && trims != null // Here is important!
            ? Padding(
                padding: const EdgeInsets.fromLTRB(25.0, 10.0, 25.0, 10.0),
                child: SizedBox(
                  height: 400.0,
                  child: charts.LineChart(
                    series,
                    behaviors: [
                      new charts.ChartTitle(
                          AppLocalizations.of(context).translate(
                              trimOption.values.first["labels"]["label"]),
                          behaviorPosition: charts.BehaviorPosition.bottom,
                          titleStyleSpec: charts.TextStyleSpec(fontSize: 11),
                          titleOutsideJustification:
                              charts.OutsideJustification.middleDrawArea),
                      new charts.ChartTitle(
                          AppLocalizations.of(context)
                                  .translate("weather.windSpeed") +
                              " (" +
                              AppLocalizations.of(context)
                                  .translate("units.knots") +
                              ")",
                          behaviorPosition: charts.BehaviorPosition.start,
                          titleStyleSpec: charts.TextStyleSpec(fontSize: 11),
                          titleOutsideJustification:
                              charts.OutsideJustification.middleDrawArea)
                    ],
                    defaultRenderer: new charts.LineRendererConfig(
                      includeLine: true,
                      includePoints: true,
                    ),
                    animate: false,
                    animationDuration: Duration(seconds: 0),
                    defaultInteractions: false,
                  ),
                ),
              )
            : Container(),
      ],
    );