Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PopScope is being called twice #2126

Open
gidrokolbaska opened this issue Jan 10, 2025 · 0 comments
Open

PopScope is being called twice #2126

gidrokolbaska opened this issue Jan 10, 2025 · 0 comments

Comments

@gidrokolbaska
Copy link

As the title says, the PopScope's onPopInvokedWithResult is being called twice when popping the route:

reproduction steps:

void main() => runApp(NavigatorPopHandlerApp());

class NavigatorPopHandlerApp extends StatelessWidget {
  NavigatorPopHandlerApp({super.key});
  final _appRouter = AppRouter();
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _appRouter.config(),
    );
  }
}

@RoutePage()
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Page One'),
            TextButton(
              onPressed: () async {
                context.router.push(const SecondPageRoute());
              },
              child: const Text('Next page'),
            ),
          ],
        ),
      ),
    );
  }
}

@RoutePage()
class SecondPage extends StatefulWidget {
  const SecondPage({super.key});

  @override
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return PopScope(
      onPopInvokedWithResult: (didPop, result) {
        print('Im called twice...');
      },
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('Page Two'),
              TextButton(
                onPressed: () async {
                  Navigator.maybePop(context);
                },
                child: const Text('Go back'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

router config:

import 'package:auto_route/auto_route.dart';

import 'router.gr.dart';

@AutoRouterConfig(replaceInRouteName: 'Page,Route,Screen')
class AppRouter extends RootStackRouter {
  @override
  List<AutoRoute> get routes => [
        AutoRoute(
          page: HomePageRoute.page,
          initial: true,
        ),
        AutoRoute(
          page: SecondPageRoute.page,
        ),
      ];
}

Video:

2025-01-10.12.44.59.mov

Example project is on the latest version of auto_route and auto_route_generator but I was also able to reproduce the error in a production app that uses version 8.1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant