Test with GoRouter navigation

Flutter tips Published on

Sometimes you need to make sure your Flutter app's navigation is working correctly, especially with GoRouter.

Testing widgets directly is often simpler, but testing navigation flow can be useful for certain scenarios.

How to Test GoRouter Navigation

Here’s a simple way to set up tests for your GoRouter navigation:

Steps

  1. Embed your router within a Material app: Wrap your GoRouter setup inside a MaterialApp.router. This provides the necessary context for navigation.
  2. Add your pages within routes: Define the routes and the widgets (pages) they lead to in your GoRouter configuration.
  3. Run your app within your test: Use testWidgets to pump your MaterialApp.router widget into the test environment.
  4. Check your expectations: Use expect to verify the widgets or content that should appear after navigation, or test specific behaviors like redirects.

Additional Tip

To test specific parts of your navigation or start your test from a particular screen, you can pass an initialLocation parameter when creating your GoRouter. Embedding the router creation logic within a function that accepts initialLocation makes this flexible for tests.

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

Why would I test GoRouter navigation?

While widget tests are common, testing navigation helps ensure that routes are correctly defined and that redirects or specific navigation flows work as expected in a near-real environment.

What is the basic setup for testing GoRouter?

You typically embed your GoRouter configuration within a `MaterialApp.router` and then use `testWidgets` to run this app setup in the test environment.

How can I start a test from a specific page?

You can create your GoRouter inside a function that takes an `initialLocation` parameter. Pass the desired route path (e.g., '/page2') to this parameter when setting up your test.

Read more
You may also be interested in
Go Beyond Material: Add Custom Colors in your Flutter Theme  blog card image
Go Beyond Material: Add Custom Colors in your Flutter Theme
Published on 2025-05-12T08:49:42.146Z
Draw and animate a round progress Using a Custom Painter  blog card image
Draw and animate a round progress Using a Custom Painter
Published on 2025-05-19T07:11:34.304Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved