Skip to content

Support go_router / MaterialApp.router(…) #48

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

Merged

Conversation

IVLIVS-III
Copy link

Fixes #47

This allows combining go_router / MaterialApp.router(…) with the DialogReportMode.
It is achieved by exposing the _configureNavigatorKey instance method of Catcher2 publicly to enable updating the used navigatorKey after runApp was called.

Now users can call:

final _router = GoRouter(…);
Catcher2.getInstance().configureNavigatorKey(_router.routerDelegate.navigatorKey);

…er `runApp` was called.

This allows combining `go_router` / `MaterialApp.router` with the `DialogReportMode`.
@ThexXTURBOXx
Copy link
Owner

Thanks for the PR - however, I am not quite sure whether I understand the current limitations.
Shouldn't it just be possible to do something like this?

void main() {
  final _router = GoRouter(...);
  [...other setup for catcher_2...]
  Catcher2(rootWidget: MyApp(), debugConfig: debugOptions, releaseConfig: releaseOptions,
    navigatorKey: _router.routerDelegate.navigatorKey); // this line right here
}

@IVLIVS-III
Copy link
Author

Thanks for the PR - however, I am not quite sure whether I understand the current limitations. Shouldn't it just be possible to do something like this?

void main() {
  final _router = GoRouter(...);
  [...other setup for catcher_2...]
  Catcher2(rootWidget: MyApp(), debugConfig: debugOptions, releaseConfig: releaseOptions,
    navigatorKey: _router.routerDelegate.navigatorKey); // this line right here
}

Yes, this would be possible, however there is another limitation.
This partial solution forces you to initialize the GoRouter before calling runApp(…) / creating the Catcher2 instance.

But imagine the following scenario:

You want to display a loading screen / sign in screen first, but catch errors with loading / sign in with Catcher2.
You need data after loading / sign in to configure the routes of your GoRouter.
This forces you to create the GoRouter instance not in main but in some widget down the line.
Now you cannot provide the navigatorKey of GoRouter to Catcher2 because Catcher2 was already created before.

See this example:

void main() {
  // [...other setup for catcher_2...]
  Catcher2(rootWidget: MyLoader(), debugConfig: debugOptions, releaseConfig: releaseOptions);  // <-- cannot access `GoRouter` instance here, because it is created later
}

class MyLoader extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: …,  // <-- do some async loading call here, maybe even sign-in
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return MyApp(isAdmin: snapshot.requireData.isAdmin);  // launch the main app when future has completed
        }
        
        return Loader();  // show loader until future completes and we can launch the main app
      },
    );
  }
}

class MyApp extends StatelessWidget {
  const MyApp({required this.isAdmin});

  final bool isAdmin;
  
  @override
  Widget build(BuildContext context) {
    final goRouter = GoRouter(
      routes: [
        GoRoute("/home", …),
        if (isAdmin) GoRoute("/admin", …),  // <-- this depends on the result of the loading call, however, I want to show a loading screen and have `Catcher2` catch loading errors before
      ],
    );
    return MaterialApp.router(
      router: goRouter;
    );
  }
}

@ThexXTURBOXx
Copy link
Owner

Ah, I see. Indeed, that's an interesting use case to cover. I believe, the best way to avoid duplicate code and the like would be to just rename the _navigatorKey field to navigatorKey to make it publicly available and changeable.
Would that also work for you/could you change this in this PR? :)

@IVLIVS-III
Copy link
Author

Yes, making _navigatorKey directly public by renaming would solve the usecase as well.
I'll change the PR accordingly.

@ThexXTURBOXx
Copy link
Owner

Thank you very much! I like that - making the field non-null is even better than it was before :)

@ThexXTURBOXx ThexXTURBOXx merged commit 8359bf3 into ThexXTURBOXx:master Apr 29, 2025
1 check passed
@ThexXTURBOXx
Copy link
Owner

Thanks once again! I will publish this now as part of 2.1.3!

dklenowski pushed a commit to dklenowski/catcher_2 that referenced this pull request Jun 4, 2025
* Exposed `configureNavigatorKey` to update the used `navigatorKey` after `runApp` was called.

This allows combining `go_router` / `MaterialApp.router` with the `DialogReportMode`.

* Refactored to remove abstractions.
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

Successfully merging this pull request may close these issues.

Support go_router / MaterialApp.router(…)
2 participants