Secure your pages

Flutter tips Published on

Protecting your app's pages is crucial. You want to make sure users only see content they are allowed to access. This often means redirecting them to a different page, like an onboarding screen, if they haven't completed certain steps or don't have the right permissions.

Redirect Users Based on Access

Imagine a scenario where a user needs to complete an onboarding process before accessing the main parts of your app. We can achieve this by creating special 'guards' that check user status and redirect them if needed.

Building a User Info Guard

We can create a UserInfosGuard widget that wraps around your page content. This guard will do a couple of things:

How a General Guard Works

A more general Guard can be used for various conditions. It takes a future that resolves to a boolean (canActivate). If canActivate is false, it triggers a redirect.

Handling Multiple Conditions

For complex situations with many checks, you can use multiple guards (cascade guards) or combine all your conditions into one comprehensive check.

Integrating Guards with goRouter

When using a routing package like goRouter, you can easily add your guards directly to your route definitions.

Adding Your Guard to a GoRoute

Inside your GoRoute setup, you'll place your UserInfosGuard as the child of the route. Specify the fallbackRoute that users should be redirected to if they don't meet the conditions.

For example, if the user hasn't onboarded, they will be sent to the /onboarding route instead of the 'home' page.

The Redirection Process

The redirection logic ensures a smooth user experience. When a redirect is triggered:

This approach helps you launch your app with confidence, knowing your pages are secure and users are guided through the correct flows.

ApparenceKit provides a Flutter template designed to help you launch your app at light speed, often including robust solutions for page security.

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 the main purpose of securing pages in a Flutter app?

The main purpose is to control access, ensuring users only view content they are authorized to see or have completed necessary steps (like onboarding) to access. It prevents unauthorized access and guides users through required flows.

How do I redirect users if they don't have access to a page?

You can use 'guards' that check specific conditions (e.g., user logged in, onboarding complete). If the conditions are not met, the guard redirects the user to a 'fallback route' using navigation methods like `pushReplacement`.

What is a 'Guard' in this context?

A 'Guard' is a component (often a widget) that sits between your route and your page content. It checks certain conditions before allowing the user to view the page. If conditions aren't met, it redirects them.

How do I integrate these guards with `goRouter`?

You integrate guards by placing them as a child of your `GoRoute` definition. You pass them a `fallbackRoute` which is the destination if the guard's conditions fail.

What happens if a user is already on the fallback route?

The redirection logic typically includes a check to see if the current route is already the `fallbackRoute`. If so, it skips the redirection to prevent infinite loops and unnecessary navigation.

What is ApparenceKit?

ApparenceKit is mentioned as a Flutter template designed to help you launch your app quickly, often providing solutions for common app needs like page security.

Why not use GoRouter redirect directly for page protection?

It's a matter of preference and flexibility. Using guards as widgets allows for more complex logic, better separation of concerns, and easier testing. It also enables reusability across different routes and scenarios. I also find it more readable and maintainable in larger applications.

Read more
You may also be interested in
Getting device timezone  blog card image
Getting device timezone
Published on 2025-11-28T16:54:52.958Z
Render widget outside of parent bounds  blog card image
Render widget outside of parent bounds
Published on 2025-11-28T16:57:11.793Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved