I would like to ask you about PopScope and go_router. I'm affraid it should have any issue

I’m trying to catch a PopScope event. I’ve tried but without success. Here is my code. I’ll simplify it a bit:

 GoRoute(
    path: '/group/:groupId',
    builder: (context, state) {
      final groupId = state.pathParameters['groupId']!;
      final extras = state.extra as Map<String, dynamic>?;

      logger.t('NR.goRoute: Grupo: $groupId. Extras recibidos: ${(extras == null) ? null : extras['fr']}');

      return PopScope(
        onPopInvokedWithResult: (didPop,_) async {
          logger.w('This log is not printed');
          if (!didPop & context.mounted) {
            context.go('/groups'); // Navegar de vuelta a /groups
          }
        },
        child: Group__Screen(
          groupId: groupId,
          groupName: extras?['groupName'] ?? '',
          groupColor: extras?['groupColor'] ?? Colors.blue,
          fr: extras?['fr'],
        ),
      );
    },
  ),

I’ve develop a button to get back, and it works fine. But for some reason, I can’t use the “back” method Android native, button or gesture.

I don’t know why it didn’t work. There is no more PopScope that can be truncate that PopScore. There is no Navigation.go() or whatelse. This is the develop of my button, that works fine.

@override
Widget build(BuildContext context) {
  final bloc = context.read<Group__Bloc>();

  return Scaffold(
    appBar: AppBar(
      title: Text(groupName),
      backgroundColor: groupColor,
      leading: IconButton(
        icon: const Icon(Icons.arrow_back),
        onPressed: () async => context.go('/groups'),
      ),
      actions: [
        IconButton(
          icon: const Icon(Icons.share),
          onPressed: () => _handleDeepLinkShare(context),
        ),
      ],
    ),
    body: Column(
      children:

That icon works fine. But back gesture doesn’t even print the log with Warning level.

I’m trying to use a phisical Android Pixel 7 to debug, in a Mac Air. I don’t know if it’s related with it, but I don’t know what I can do more.

I’ve tried to disable the back gesture in order to keep the button as only option to go back, but without success.

Please, be gentle. I’m trying to develop an app to obtain some benefits with A LOT OF migraines. Right now I have one. 26 per month. My entire life is chaotic. I’ve jump to develop some ideas I have for new apps with the help of Claude AI, but this problem is too difficult for it and for me.

Any idea to avoid PopScope or make it work properly?

A friend told me it doesn’t understand well. The native “back” button from Android as well as the “back” gesture, slicing from the lateral to the center, both methods skip the app. But the “back” button develop with Flutter works fine. I want to know if there is any solution for what I pretend.

Before anybody ask again (like in Stackoverflow), yes, I’ve tried to use canPop, without success. I’ve tried all I’ve seen. WillPopScope, PopScope, custom NavigationHandler widget, onPopInvoked, onPopInvokedWithResult, canPop…

I think it is solved. It was related with the way I define the routes relations. There is more complicated than I thought. I need to check my new theory, as soon as I confirm it, I’ll close this ask. Sorry for bored you.

Yes. That’s right. I need to use GoRouted nested to navigate from one path to the previous one and so on

GoRoute(
          path: '/groups',
          builder: (context, state) => const PopScope(
            canPop:false,
            child: GroupsScreen(),
          ),
          routes: [
            GoRoute(
              path: 'avatar-selector',
              builder: (context, state) => PopScope(
                onPopInvokedWithResult: (didPop, result) {
                  if (didPop) {
                    logger.d('Popped from group route');
                  } else {
                    logger.d('Popped from group route with result: $result');
                  }
                },
                child: const AvatarSelectorScreen(),
              ),
            ),
            GoRoute(
              path: ':groupId',
              builder: (context, state) {
                final groupId = state.pathParameters['groupId']!;
                final extras = state.extra as Map<String, dynamic>?;
                logger.d('Building group route for $groupId');
                return PopScope(
                  onPopInvokedWithResult: (didPop, result) {
                    if (didPop) {
                      logger.d('Popped from group route'); // I finally see this log!!!!
                    } else {
                      logger.d('Popped from group route with result: $result');
                    }
                  },
                  child: Group__Screen(...),
                );
              },
            ),
          ],
        ),
1 Like