RSS Amplifier

Aaron Arney's RSS Feed · Dec 21, 2019

Named Routes in Flutter

0
Sign in to vote or save

This page did not load. You can still read it on the original site — the toolbar below keeps your place in the directory.

Flutter provides built-in functionality for routing in apps. You can use either pass the MaterialApp a Widget, or a named route. There are…

Flutter provides built-in functionality for routing in apps. You can use either pass the MaterialApp a Widget, or a named route.

There are two ways of going about named routes, you can either:

  • Use URL style routes /main or /login etc.
  • Use an arbitrary string loginscreen, mainscreen etc.

When using URL style identifiers, you must provide a default route of / or the app will crash. Personally, I don’t like this style at the moment, but in the future when Flutter web support is stable this may make more sense to me.

The issue with using strings as ID’s is that static code analysis won’t catch typos. Meaning if you have a loginscreen route but then attempt to use that route later in your app, but accidentally type lognscreen, the IDE won’t catch that.

Instead, in each of your screen widgets, I think it’s a good idea to add a static id to the class, so that you can reference it wherever you need without having to worry about getting the string correct every time.

class LoginScreen extends StatefulWidget {
  static const String id = 'login_screen';
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

When you create your named routes Map, pass the id’s as the key.

routes: {
  LoginScreen.id: (context) => LoginScreen(),
},

Then whenever you want to navigate to that screen, you simply access that static id.

Navigator.pushNamed(context, LoginScreen.id);

That’s it.

Read on aaronarney.dev

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.