返回前一屏幕时Flutter应用程序崩溃

时间:2020-10-24 17:22:38

标签: flutter mobile cross-platform

我有一个Flutter应用程序,该应用程序可以填充河表信息列表,并允许用户使用charts_flutter包选择项目并在图表中查看详细信息。

我过去在FutureBuilder上成功地使用了该图表,现在将其重构为使用Provider。包含图表的视图按预期工作,但是返回上一屏幕(仪表列表)时,列表显示得非常简短后,我收到一条错误消息。错误如下。

enter image description here

这是呈现图表视图的代码

class GaugeDetailChart extends StatefulWidget {
  GaugeReferenceModel referenceModel;
  GaugeDetailChart({this.referenceModel});

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

class _GaugeDetailChartState extends State<GaugeDetailChart> {
  GaugeDetailViewModel viewModel;

  @override
  void initState() {
    _loadData();
    super.initState();
  }

  _loadData() {
      viewModel = Provider.of<GaugeDetailViewModel>(context, listen: false);
      viewModel.reloading = true;
      viewModel.setReferenceModel(widget.referenceModel);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: RLAppBar(
          titleText: Text('Gauge Detail'),
        ),
        body: Center(
          child: Consumer<GaugeDetailViewModel>(
            builder: (context, model, child) => Stack(
              children: [
                Center(
                  child: Visibility(
                      visible: model.reloading,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          CircularProgressIndicator(),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text('Loading details for'),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text('${widget.referenceModel.gaugeName}'),
                          )
                        ],
                      )),
                ),

                // removing the following container resolves the exception

                Container(
                    height: MediaQuery.of(context).size.height * .5,
                    alignment: Alignment(0.0, 0.0),
                    color: Colors.lightBlueAccent,
                    child: charts.TimeSeriesChart(
                      model.seriesFlowData,
                      animate: true,
                      animationDuration: Duration(milliseconds: 500),
                      primaryMeasureAxis: charts.NumericAxisSpec(
                          tickProviderSpec: charts.BasicNumericTickProviderSpec(
                              zeroBound: false,
                              dataIsInWholeNumbers: false,
                              desiredMaxTickCount: 8,
                              desiredMinTickCount: 5),
                          renderSpec: charts.GridlineRendererSpec(
                              tickLengthPx: 0, labelOffsetFromAxisPx: 5)),
                    )
                )
              ],
            ),
          ),
        ));
  }
}

该图表可以正确显示在视图上。唯一的错误发生是在退出该视图并返回到上一个视图时,该操作是使用NavigationController后退按钮执行的。

如果我删除了保存图表的容器,则返回上一个视图将正常工作。关于容器的某些信息导致先前的视图失败。产生的错误非常模糊。

The following assertion was thrown building Overlay-[LabeledGlobalKey<OverlayState>#5f9d5](state: OverlayState#a6169(entries: [OverlayEntry#2b4ce(opaque: true; maintainState: false), OverlayEntry#eea3d(opaque: false; maintainState: true), OverlayEntry#c217d(opaque: true; maintainState: false), OverlayEntry#ae45e(opaque: false; maintainState: true), OverlayEntry#c7b73(opaque: true; maintainState: false), OverlayEntry#e34a6(opaque: false; maintainState: true)])):
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5125 pos 14: '_dependents.isEmpty': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

The relevant error-causing widget was: 
  MaterialApp file:///Users/philiptownsend/Documents/projects.nosync/_Flutter/RiverLink/stream_watcher/lib/main.dart:32:12
When the exception was thrown, this was the stack: 
#2      InheritedElement.debugDeactivated.<anonymous closure> (package:flutter/src/widgets/framework.dart:5125:14)
#3      InheritedElement.debugDeactivated (package:flutter/src/widgets/framework.dart:5127:6)
#4      _InactiveElements._deactivateRecursively.<anonymous closure> (package:flutter/src/widgets/framework.dart:2048:15)
#5      _InactiveElements._deactivateRecursively (package:flutter/src/widgets/framework.dart:2050:6)
#6      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4710:14)

有人曾经经历过类似的经历吗?或者有人可以建议从哪里着手或如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定,但是为什么不尝试将容器设为单独的小部件并尝试 调用该小部件。也许可以解决问题