Create a page route guard

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

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

  • It takes a canActivate future (your condition check).
  • It takes the child widget (the page you want to show if the condition is true).
  • It takes a fallbackRoute (the page to go to if the condition is false).
  • It uses a FutureBuilder to wait for the canActivate future to finish.
What Happens After the Check?
  • If canActivate is true, it shows the child page.
  • If canActivate is false or there's an error, it uses Navigator.pushReplacementNamed to send the user to the fallbackRoute. The pushReplacementNamed makes sure the user can't just hit the back button to get to the restricted page.

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

One command. Pick your modules. Firebase or Supabase auto-configured. Start building what matters.

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
Made by ApparenceKit logo
Featured on Twelve Tools
ApparenceKit is a flutter start kit | template generator tool by Apparence.io © 2026.
All rights reserved