flutter:报错之: Looking up a deactivated widget's ancestor is unsafe

错误内容

[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: [
                  new FlatButton(
                    child: new Text("确定"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  )
                ]);
          });
          setState(() {
        _alertTime = _tmpAlertTime;
      });
    });
  }

2.countdownAlertTime()在此处被调用

flutter:报错之: Looking up a deactivated widget's ancestor is unsafe_第1张图片

3.showInputDialog()在此处被调用

flutter:报错之: Looking up a deactivated widget's ancestor is unsafe_第2张图片

分析 纯自己理解

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

解决方案。

在A调用时 传递 this.context 能正确指代 最初的context上下文。
flutter:报错之: Looking up a deactivated widget's ancestor is unsafe_第3张图片

结语

好了 继续去撸代码了。

你可能感兴趣的:(flutter入坑,flutter)