Conditionally loading a Flutter App’s first screen with AutoRoute and Navigator 2.0

Introduction

I’m assuming you already have auto router setup. If not, check this link to see how to set it up with navigator 2.0 in Flutter.

Build method in root widget with auto_route and navigator 2.0
final _appRouter = AppRouter();
late bool isSignedIn, isVerified, isTnCAccepted;
routerDelegate: _appRouter.delegate(
initialRoutes: [
if (!isSignedIn) LandingScreen(),
if (isSignedIn && !isTnCAccepted) PledgeScreen(),
if (isSignedIn && isTnCAccepted) DashboardScreen(),
],
),

Explanation

In my case, if the user is not signed in, it returns LandingScreen. If the user is signed in and has not accepted the terms, it takes them to the PledgeScreen, else, it takes them to the DashboardScreen.

Full Code example

Conclusion

This is how easy it was to use AutoRouter with Navigator2.0, when you have to conditionally open a page in the app. In addition to this, AutoRouter also simplifies deep-links, opening the correct page on launch with dynamic links etc. Will be covering that soon! Hope you liked my first blog here :). Would love to hear suggestions to improve.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store