Search code examples
flutterflutter-navigation

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?


Solution

  • 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