错误内容

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Looking up a deactivated widget’s ancestor is unsafe

我理解的错误原因

找不到靠谱的上下文,也就是我所传递的上下文无效了。

1.出现错误的地方

 void countdownAlertTime(BuildContext context) {
      print("进入倒计时");
    new Timer(new Duration(seconds: 2), () {
      print("进入倒计时回调中");
      Navigator.of(context).pop();  //就是这句出的问题
      print("第一次页面pop");
      showDialog(
          context: context,
          builder: (context) {
            return new AlertDialog(
                title: new Text("恭喜"),
                content: new Text("操作成功!"),
                actions: <Widget>[
                  new FlatButton(
                    child: new Text("确定"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  )
                ]);
          });
          setState(() {
        _alertTime = _tmpAlertTime;
      });
    });
  }

2.countdownAlertTime()在此处被调用

在这里插入图片描述

3.showInputDialog()在此处被调用

在这里插入图片描述

分析 纯自己理解

C =>B=>A
C被B调用,B被A调用,其中的context (BuildContext对象) 已经指代不明确了。就是报错的意思,上下文有丢失的意思。

解决方案。

在A调用时 传递 this.context 能正确指代 最初的context上下文。
在这里插入图片描述

结语

好了 继续去撸代码了。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐