Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing from WillPopScope to PopScope throws exception of '!_debugLocked': is not true

I'm changing my code from WillPopScope to PopScope to capture pressing of the back button. I need to display a dialog before going back to previous screen. So, I need to issue Navigator.pop twice, one to dismiss dialog and other one going back to previous screen. The code below works fine with WillPopScope but an error of '!_debugLocked': is not true is thrown when I change it to PopScope since WillPopScope is depreciated.

// return WillPopScope(
return PopScope(
    canPop:true,
    onPopInvoked: (bool didPop) async{
  //  onWillPop: () async {
    if (vStatus[1] && !vStatus[2]) {
      _showMaterialDialog(context, 1);
    }
    return Future.value(true);
  },
  child: Scaffold(
   …….


void _showMaterialDialog(BuildContext context, int opt) {
  showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title:  Text(getTextTitle(opt),style:TextStyle(fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0)),
          content: Text(getTextContent(opt),
              style: const TextStyle(
                  fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),
              ),
          actions: <Widget>[
            TextButton(
                onPressed: ()  {
                    Utils.saveVideo().then((value) async {
                     if(value!) {
                        vStatus[2] = true;
                    }
                  });
             
                    Navigator.of(context).pop(); //dismiss dialog
                    Navigator.of(context).pop(); //go back to previous screen             
            
                },
                child: const Text(
                    style: TextStyle(
                        fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),
                    'Yes')),
            TextButton(
              onPressed: () {
              
                 Navigator.of(context).pop(); //dismiss dialog
                 Navigator.of(context).pop(); //go back to previous screen  
               
                }
              },
              child: const Text(
                  style: TextStyle(
                      fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0),
                  'No'),
            )
          ],
        );
      });
}

Appreciate any feedback!

like image 898
jdevp2 Avatar asked Dec 31 '25 23:12

jdevp2


1 Answers

According to release notes https://docs.flutter.dev/release/breaking-changes/android-predictive-back#migrating-a-back-confirmation-dialog:~:text=route%20transitions.%0A%7D-,Migrating%20a%20back%20confirmation%20dialog,-WillPopScope%20was%20sometimes

You can use the below code

  PopScope(
  canPop: false,
  onPopInvokedWithResult: (bool didPop, Object? result) async {
    if (didPop) return;
    final NavigatorState navigator = Navigator.of(context);
    if (vStatus[1] && !vStatus[2]) {
      await _showMaterialDialog(context, 1);
    }
    navigator.pop();
  },
),
like image 110
Abhishek Karad Avatar answered Jan 06 '26 12:01

Abhishek Karad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!