yN.
yN.

Reputation: 2257

Navigator.push() vs Navigator.pushNamed() when passing data to screen

I would like to pass data to another screen. According to the docs when using named routes I need to use Arguments and use:

Navigator.pushNamed(
  context,
  NextScreen.route,
  arguments: NextScreenArgs("pew"),
);

However the same(?) could be accomplished by just using:

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => NextScreen("pew"),
  ),
);

Is there any difference or advantage using pushNamed?

Upvotes: 5

Views: 9873

Answers (2)

Hamza Farooq
Hamza Farooq

Reputation: 69

Navigator.push() method works when you have only two or three routes. But when it comes to complex apps, you might have multiple routes. In that case, if we use Navigator.push() method then it will result in a lot of code duplication.

Upvotes: 6

Yauhen Sampir
Yauhen Sampir

Reputation: 2104

Flutter team has a discussion about routing on github. I like this explanation:

While navigation without using named routes is OK for smaller projects, in more complex apps it adds code duplication. This is especially true if you have a route guard to only allow signed-in users enter certain pages, or any other kind of logic which needs to run as the user navigates.

Also you can read more discussions here: https://github.com/flutter/flutter/issues/3867

Upvotes: 17

Related Questions