首页 > 解决方案 > 无法从抽屉中关闭 alertDialog

问题描述

我正在尝试单击showDialog元素时drawer

ListTile(
      title: Text('Home'),
      leading: Icon(Icons.home_outlined),
      onTap: () {
        Navigator.pop(context);
        showDialog(
            context: context,
            child: AlertDialog(
              title: Text('Confirm Sign out'),
              content: Text('Are  sure to sign out from app now?'),
              actions: [
                FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Cancel'),
                ),
              ],
            ));
      },
    ),

小部件的完整代码drawer

class HomeDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return Drawer(
    // your widgets goes here
    child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    ListTile(
      title: Text('Notifications'),
      leading: Icon(Icons.notifications_outlined),
      onTap: () {},
      selected: true,
    ),
    Divider(),
    ListTile(
      title: Text('Logout'),
      leading: Icon(Icons.logout),
      onTap: () {
        Navigator.pop(context);
        showDialog(
            context: context,
            child: AlertDialog(
              title: Text('Confirm Sign out'),
              content: Text('Are  sure to sign out from app now?'),
              actions: [
                FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Cancel'),
                ),
                FlatButton(
                  onPressed: () async {
                    await FirebaseAuth.instance.signOut();
                  },
                  child: Text('Yes and Confirm'),
                )
              ],
            ));
      },
    ), //here is a divider
    Spacer(),
  ],
));
}
 }

但是,dilaog并没有解雇,它显示以下错误:

Looking up a deactivated widget's ancestor is unsafe.

At this point the state of the widget's element tree is no longer stable.

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.

When the exception was thrown, this was the stack: 
#0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3906:9)
#1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3920:6)
#2      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4039:12)
#3      Navigator.of (package:flutter/src/widgets/navigator.dart:2218:40)
#4      Navigator.pop (package:flutter/src/widgets/navigator.dart:2107:15)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#016ce
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(140.5, 462.5)
  finalLocalPosition: Offset(9.5, 34.0)
  button: 1
  sent tap down

标签: flutterdart

解决方案


Navigator.pop(context)从中删除onTap

为什么它不起作用?

因为这个当前小部件会从小部件树中弹出,所以在该上下文之后,当前小部件也将不可用。

onTap: () {
           Navigator.pop(context); // Remove this to pop from here
          // Rest code 
       }

推荐阅读