jhomlala / catcher

Flutter error catching & handling plugin. Handles and reports exceptions in your app!
Apache License 2.0
785 stars 177 forks source link

[FEATURE] Support MaterialApp.router() which doesn't have a navigatorKey argument, as well as MaterialApp() #197

Open buzzware opened 3 years ago

buzzware commented 3 years ago

Is your feature request related to a problem? Please describe. Your documentation says to include navigatorKey: Catcher.navigatorKey when calling MaterialApp(). Navigator 2.0 uses MaterialApp.router instead, which doesn't have a navigatorKey parameter, and then I get the error "Couldn't use report mode because you didn't provide navigator key. Add navigator key to use this report mode."

Describe the solution you'd like I don't know enough about keys or this library to suggest a solution

Describe alternatives you've considered Maybe create my own version of MaterialApp.router ?

Additional context Add any other context or screenshots about the feature request here.

buzzware commented 3 years ago

The issue occurs with DialogReportMode. Using SilentReportMode instead seems to resolve it.

jhomlala commented 2 years ago

@buzzware Navigator key has been used for 1.0 routing. I didn't use 2.0 yet because level of complexity (there's even package created by one of flutter team members to handle this complexity). Can you provide some minimal reproducible example? It could be just small two screens app.

mikeesouth commented 2 years ago

@jhomlala I agree that the 2.0 routing is a mess from Flutters side, at least earlier this year when I implemented it in our app. This is a pretty good article and there are some others too: https://johnwcassidy.medium.com/building-a-simple-stack-navigator-with-flutter-navigation-2-0-f11b55b71520 That article has the source code for the navigator 2.0 sample here: https://github.com/bigbluewhaledev/flutter_navigation_2_0

I'm using catcher with navigator 2.0 by adding the Catcher.navigatorKey to the "Navigator" widget, like this: final navigatorKey = Catcher.navigatorKey ?? GlobalKey<NavigatorState>();

And then I set it in the Navigator class like this:

return Navigator(
  key: navigatorKey,
  pages: [...],
  ....,
);

I'm just adding catcher to this project but it seems to work very nicely.

@buzzware see above if you haven't found a workaround yet.

ibrahim-mubarak commented 2 years ago

I think that the larger issue here is some routing libraries are managing their own navigator keys. As catcher is usually initialised very early on in the App setup, and the routers are probably initialised in the runAppFunction or rootWidget section of catcher, there is no place where a navigator key can be setup.

I think this can be mitigated by either the navigator libraries allowing us to provide a Key or Catcher allowing us to provide a Navigator key later on in the App Setup cycle

Just through a quick skim, VRouter, Auto Route, Flutter Modular allow setting a NavigatorKey Go Router and Beamer do not

rivella50 commented 2 years ago

@ibrahim-mubarak So is there another way to set the value for navigatorKey? I would like to use Beamer but do not see a way to apply GlobalKey<NavigatorState>.

fredgrott commented 1 year ago

For Go Router in your GoRouter constructor you can set a navigationKey so that is the place to put the Catcher.naviationKey as far as I know