Create a page route guard

Flutter tips Published on

Building apps often means controlling who can see what pages. Sometimes, you need to check something, like if a user is logged in, before showing them a specific screen.

What is a Page Route Guard?

A page route guard is like a security check for your pages. Before a user lands on a page, the guard runs a check. If the check passes (the condition is true), the user sees the page. If the check fails (the condition is false), the user is sent somewhere else, like a login page.

Why Use a Guard?

Guards help you keep parts of your app safe and control the user flow. They are great for protecting pages that require a user to be logged in, have certain permissions, or have completed specific setup steps.

How Does it Work?

The image shows a simple Guard widget. This widget checks a condition using a Future<bool> canActivate. A Future is used because checking a condition might take some time, like fetching data from the internet.

Here's the basic idea:

The Guard Widget Logic

What Happens After the Check?

Using the Guard in Your Routes

You can wrap the widget for a specific route with your custom guard, like an AuthenticatedGuard.

Route<dynamic> route(RouteSettings settings) {
  switch (settings.name) {
    case 'init_account':
      return MaterialPageRoute(
        builder: (_) => AuthenticatedGuard(child: InitAccountPage()),
      );
    // ... other routes
  }
}

This way, the InitAccountPage will only be shown if the AuthenticatedGuard's condition is met. Otherwise, the user will be redirected.

Handling Multiple Conditions

What if you need to check more than one thing? You have a couple of options:

Option 1: Cascade Guards

You can wrap a page with multiple guards, each checking a different condition. If any guard's condition fails, the user is redirected.

Option 2: Compile Conditions

You can combine all your conditions into a single canActivate future within one guard. This might make the logic cleaner but requires more complex logic inside the guard itself.

Using page route guards is a clean way to manage access control and navigation flow in your Flutter app, making it more robust and secure.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a page route guard in Flutter?

A page route guard is a way to check a condition before allowing a user to access a specific page. If the condition is false, the user is redirected to another page.

Why should I use a route guard?

Route guards help protect pages that require certain conditions, like user authentication or specific permissions, ensuring users only see pages they are allowed to access.

How does the Guard widget in the example work?

The Guard widget checks a `Future<bool>` condition (`canActivate`). If the condition is true, it shows the main page (`child`). If it's false or errors, it redirects the user to a `fallbackRoute`.

How do I handle checking multiple conditions for a page?

You can either wrap the page with multiple guards (cascading) or combine all your checks into a single complex condition within one guard.

Will the user be able to go back to the protected page after being redirected?

No, the example uses `Navigator.pushReplacementNamed`, which replaces the current page in the navigation stack, preventing the user from navigating back to the protected page using the back button.

Read more
You may also be interested in
Change flutter version  blog card image
Change flutter version
Published on 2025-05-12T12:23:31.879Z
Delegate widget design  blog card image
Delegate widget design
Published on 2025-05-12T09:03:59.853Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved