【问题标题】:Flutter app crashes when returning to previous screenFlutter 应用程序在返回上一个屏幕时崩溃
【发布时间】:2021-02-07 11:18:21
【问题描述】:

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

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

这是呈现图表视图的代码

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 后退按钮执行的。

如果我删除了保存图表的容器,则返回上一个视图不会出错。关于 Container 的某些东西导致先前的视图失败。产生的错误非常模糊。

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)

以前有没有人经历过这样的事情,或者任何人都可以就从哪里开始寻找或如何解决这个问题提出建议?

谢谢!

【问题讨论】:

    标签: flutter mobile cross-platform


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但我已经尝试过两种方法,结果相同。此时真的只是在黑暗中拍摄。
    • 同样的问题..你解决了吗? @Pheepster
    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多