Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ravgeet Dhillon
Ravgeet Dhillon

Posted on • Originally published atravsam.in on

     

Dynamic Home Route in a Flutter App

Dynamically decide the home page to be shown to a user in a Flutter App based on some authentication logic.

You can also read this article directly onRavSam’s blog. We publish our articles on Medium after a week.

In any production app, the user is directed to a route based on some authentication logic whenever the app is opened. In our Flutter App, we have at least two routes,Login andDashboard. The problem is how can we decide which route should a user be redirected to?

In this app, we will check the value of a locally stored boolean variable to dynamically decide the home route. We can use any method for writing our authentication logic, like checking the validity of the API token, but for the sake of simplicity, we will explore a simple logic.

Flutter Dynamic Home Route
Flutter Dynamic Home Route Flowchart

Contents

  • 1. Installing Dependencies
  • 2. Writing Code
  • Results

1. Installing Dependencies

In our pubspec.yaml, let us add the following dependencies that we will be using in our Flutter application:

dependencies:shared_preferences:^0.5.12+4async:^2.4.2
Enter fullscreen modeExit fullscreen mode

Make sure to install the latest version of the dependencies.

Shared Preferences is a simple Flutter plugin for reading and writing simple key-value pairs to the local storage.Async contains the utility functions and classes related to thedart:async library.

After adding these dependencies, it is now time to install them. In the terminal, let us execute the following command:

flutter pub get
Enter fullscreen modeExit fullscreen mode

2. Writing Code

In our main.dart, let us add the following code:

import'package:flutter/material.dart';import'package:shared_preferences/shared_preferences.dart';voidmain()async{// handle exceptions caused by making main asyncWidgetsFlutterBinding.ensureInitialized();// init a shared preferences variableSharedPreferencesprefs=awaitSharedPreferences.getInstance();// get the locally stored boolean variableboolisLoggedIn=prefs.getBoolean('is_logged_in');// define the initial route based on whether the user is logged in or notStringinitialRoute=isLoggedIn?'/':'login';// create a flutter material app as usualWidgetapp=MaterialApp(...initialRoute:initialRoute,);// mount and run the flutter apprunApp(app);}
Enter fullscreen modeExit fullscreen mode

The code is pretty self-explanatory. All we are doing is getting the value of is_logged_in boolean variable, and then decide the value of the initialRoute in our Flutter Material App.

One important thing in the above code is the use of theasync-await pattern. We can also use then but it makes the code a little messy and that’s what we are trying to avoid here. Making our main() function asynchronous can cause some exceptions, so to solve this, we need to add WidgetsFlutterBinding.ensureInitialized().

Results

That’s it. We have successfully written a code that allows us to redirect the user to theDashboard page if they are logged in, otherwise to theLogin page. If you any doubts or appreciation for our team, let us know in the comments below.

About RavSam Web Solutions

We are helping companies and startups to migrate their Web and Mobile App to JAMstack architecture.Reach out to us to know more about our services, pricing, or anything else.

You might also enjoy reading


Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full Stack Developer / Technical Content Writer
  • Location
    India
  • Education
    M. Tech in Computer Science and Engineering
  • Work
    Software Engineer at CloudAnswers
  • Joined

More fromRavgeet Dhillon

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp