androidflutterflutter-navigation

Popping Navigation Stack with android back button


I am trying to navigate backwards in my flutter app using the android back button, the issue being that I can't pop the navigation and pass data like the traditional pop method. I have been able to do this fine using the normal button on press but I don't want to disable the back button incase it passes data incorrectly.

Navigator.pop(context, {'state': 'update'});

I looked into and attempted using the NavigatorPopHandler but this function doesn't even get called when I try and return to the previous page.

I also tried using the PopScope class but this only returns errors but will print debug statements, when I get it to run after a bit of time/interaction with the app this is the error I get

════════ Exception caught by gesture ═══════════════════════════════════════════ The following assertion was thrown while handling a gesture: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4782 pos 12: '!\_debugLocked': is not true.

any help would be great


Solution

  • This was the solution I found, I believe by blocking the canPop and checking if the navigation has popped it removes the default race condition. Hope this helps someone. Flutter PopScore Class

    return PopScope(
          canPop: false,
          onPopInvoked: (didPop) async {
            if (didPop) {
              return;
            }
            debugPrint("here");
            Navigator.pop(
                context, {'state': 'update'});
          },