ka_sh
ka_sh

Reputation: 607

Is it a bad practice to use navigatorKey in flutter?

While I'm searching for a solution to navigate to some widget without context (my use case involves navigating to a widget onNotificationClick events). I found a solution to create a GlobalKey<NavigatorState>() and use it with MaterialApps navigatorKey property to navigate wherever I want.

It is working well for me, however recently I came across some comments in SO where it is said not to use this way because global keys are expensive but I couldn't find a complete answer to this question. So, is it really a bad practice, what can be an alternate solution?

Upvotes: 0

Views: 327

Answers (1)

Giorgi
Giorgi

Reputation: 137

Flutter 1.22 introduced Navigator 2.0, a new declarative API that allows you to take full control of your navigation stack

  1. Exposing the navigator’s page stack: You can now manage your pages

  2. Backward-compatible with imperative API: You can use both imperative and declarative styles in the same app.

  3. Handle operating system events: Works better with events like the Android system’s Back button.

Router( routerDelegate: _appRouter, backButtonDispatcher: RootBackButtonDispatcher(), )),

  1. Manage nested navigators: Gives you control over which navigator has priority.

  2. Manage navigation state: Lets you parse routes and handles web URLs and deep linking

Upvotes: 0

Related Questions