Dynamic Home Route in a Flutter App
Tags: mobileapps, flutter
Published: 2021-01-15 07:25:47

---
title: Dynamic Home Route in a Flutter App
published: true
date: 2021-01-14 06:58:19 UTC
tags: mobileapps,flutter
canonical_url: https://www.ravsam.in/blog/dynamic-home-route-in-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 on [RavSam’s blog](https://www.ravsam.in/blog/dynamic-home-route-in-flutter-app/). 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** and **Dashboard**. 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](https://cdn-images-1.medium.com/max/1024/0*zPqYVaXrucQ2Ckwm.png)<figcaption>Flutter Dynamic Home Route Flowchart</figcaption>

#### 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:

```yml
dependencies:
  shared_preferences: ^0.5.12+4
  async: ^2.4.2
```

> Make sure to install the latest version of the dependencies.

[Shared Preferences](https://pub.dev/packages/shared_preferences) is a simple Flutter plugin for reading and writing simple key-value pairs to the local storage. [Async](https://pub.dev/packages/async) contains the utility functions and classes related to the _dart:async_ library.

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

```bash
flutter pub get
```

#### 2. Writing Code

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

```dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
  // handle exceptions caused by making main async
  WidgetsFlutterBinding.ensureInitialized();

  // init a shared preferences variable
  SharedPreferences prefs = await SharedPreferences.getInstance();
  
  // get the locally stored boolean variable
  bool isLoggedIn = prefs.getBoolean('is_logged_in');
  
  // define the initial route based on whether the user is logged in or not
  String initialRoute = isLoggedIn ? '/' : 'login';

  // create a flutter material app as usual
  Widget app = MaterialApp(
    ...
    initialRoute: initialRoute,
  );

  // mount and run the flutter app
  runApp(app);
}
```

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 the _async-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 the **Dashboard** page if they are logged in, otherwise to the **Login** 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](https://www.ravsam.in) to know more about our services, pricing, or anything else.

#### You might also enjoy reading

- [Why Flutter Developer could be a $1Mn job?](https://www.ravsam.in/blog/flutter-developer-could-be-a-usd-1million-job/)
- [Top Flutter plugins to take your app to next level](https://www.ravsam.in/blog/top-flutter-plugins-to-take-your-app-to-next-level/)

* * *