Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.

一、具体错误:

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

E/flutter ( 2631): At this point the state of the widget's element tree is no longer stable.

E/flutter ( 2631): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

 

二、描述

项目中在主界面发起弹窗,然后在回调函数中又发起等待弹窗。等待弹窗的context继承自第一个弹窗。当第一个弹窗

 Navigator.pop(context);之后,他的context不可用。所以当对等待弹窗调用context的时候,会报这个出错。

                            showDialog(
                                  context: context,
                                  barrierDismissible: true,
                                  builder: (context) {
                                    return BackupDialog(
                                      sure: (type) {
                                        model.backup(type, context);
                                      },
                                    );
                                  },
                                ),    

tip:注意BackDialog的回调方法sure,其中的backup方法传的是自身的context。将其改为父context即可。如下

                                showDialog(
                                  context: context,
                                  barrierDismissible: true,
                                  builder: (_) {
                                    return BackupDialog(
                                      sure: (type) {
                                        model.backup(type, context);
                                      },
                                    );
                                  },
                                ),

三、总结

当出现该问题的时候,是由于context已经被销毁,变得不可用。请检测后面执行的代码是否依赖了销毁的context。

你可能感兴趣的:(flutter)