0

Why can't CupertinoDialog pop when Navigator.pop(context) is called together with a function using a global key?

final GlobalKey <_PlayyButtonState>_playingButtonState = GlobalKey<_PlayyButtonState>();

...
Future<void> showAlert(){
    return showCupertinoDialog<void>(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: const Text('Re-write?'),
        content: const Text('Are you sure? You will have to write from scratch.'),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            child: const Text('Re-write',
                style: TextStyle(
                    color: Colors.red
                )
            ),
            isDestructiveAction: true,
            onPressed: () {
              _playingButtonState.currentState._onToggle();
              Navigator.pop(context); <---- does not pop after calling the function above
            },
          )
        ],
      ),
    );
  }
}

Removing the code

      _playingButtonState.currentState._onToggle();

allow the CupertinoDialog to pop again

scott lee
  • 546
  • 5
  • 23

1 Answers1

1

Instead of this

 onPressed: () {
              _playingButtonState.currentState._onToggle();
              Navigator.pop(context); <---- does not pop after calling the function above
            },

Use this:

 onPressed: () {

Future.delayed(const Duration(milliseconds: 1000), () {

// Here you can write your code

 Navigator.pop(context);

});
              _playingButtonState.currentState._onToggle();
           
            },
  • Hi @Tasnuva, thanks for the reply. I realised there was a careless mistake somewhere in the code that kept calling cupertinodialog even after i closed it. I've just solved the issue. – scott lee May 11 '22 at 05:50