GoRoute#

All named parameters#

The correct and complete numbered list of named parameters of GoRoute in Flutter (go_router package). These are only the parameters that belong to the GoRoute constructor

GoRoute(

  1. path: (required) β€’ The URL path (e.g. β€˜/’, β€˜/login’, β€˜/profile/:id’)

  2. name: β€’ Optional route name for context.goNamed()

  3. builder: β€’ Builds a widget for the route

  4. pageBuilder: β€’ Builds a custom Page (advanced navigation)

  5. routes: β€’ Child/sub routes (nested routes)

  6. redirect: β€’ Redirect logic for this route only

  7. onExit: β€’ Runs when leaving the route (can prevent leaving)

  8. parentNavigatorKey: β€’ Tells the route which navigator to use (ShellRoute use)

)

βœ…1.path:#

Here is the simplest and cleanest Flutter example that demonstrates the path: parameter in GoRoute using MaterialApp.router().

I made it very short, no errors, and perfect for beginners.

βœ… Simple Program β€” Using path: in GoRoute with MaterialApp.router()

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // -------------------------------
    // GoRouter with path:
    // -------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',           // πŸ‘ˆ Home path
                builder: (context, state) => const HomePage(),
            ),
            GoRoute(
                path: '/about',      // πŸ‘ˆ About page path
                builder: (context, state) => const AboutPage(),
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
            routerConfig: _router,
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/about');   // πŸ‘ˆ navigate using path
                    },
                    child: const Text('Go to About'),
                ),
            ),
        );
    }
}

class AboutPage extends StatelessWidget {
    const AboutPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('About')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/');        // πŸ‘ˆ back to home
                    },
                    child: const Text('Go Back Home'),
                ),
            ),
        );
    }
}

βœ… Explanation (Very Simple)

πŸ”Ή path: β€˜/’

This defines the Home page.

πŸ”Ή path: β€˜/about’

This defines the About page.

πŸ”Ή Navigation

You go to a path using:

context.go('/about');

And go back:

context.go('/');

βœ…2.name:#

Here is a very simple, clean, error-free example showing how to use the name: parameter in GoRoute **with **MaterialApp.router().

βœ… Simple Program β€” Using name: in GoRoute with MaterialApp.router()

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // --------------------------------
    // GoRouter using name:
    // --------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',
                name: 'home',               // πŸ‘ˆ route name
                builder: (context, state) => const HomePage(),
            ),
            GoRoute(
                path: '/about',
                name: 'about',              // πŸ‘ˆ route name
                builder: (context, state) => const AboutPage(),
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
            routerConfig: _router,
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.goNamed('about');    // πŸ‘ˆ navigate using name
                    },
                    child: const Text('Go to About'),
                ),
            ),
        );
    }
}

class AboutPage extends StatelessWidget {
    const AboutPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('About')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.goNamed('home');     // πŸ‘ˆ navigate using name
                    },
                    child: const Text('Go Back Home'),
                ),
            ),
        );
    }
}

βœ… Explanation (Very Easy)

πŸ”Ή name: β€˜home’

Gives a name to the route.

πŸ”Ή context.goNamed(β€˜about’)

Navigate using the route name instead of URL path.

πŸ”Ή Benefits of using name:

  • Cleaner navigation

  • Easy to pass parameters

  • Avoid hard-coding URLs

  • Works great for big apps


βœ…3.builder:#

Here is the simplest possible program that demonstrates the builder: parameter in GoRoute using MaterialApp.router().

Very clean, easy, and beginner-friendly.

βœ… Simple Program β€” Using builder: in GoRoute with MaterialApp.router()

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // -------------------------------
    // GoRouter using builder:
    // -------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',
                builder: (context, state) => const HomePage(),   // πŸ‘ˆ using builder
            ),
            GoRoute(
                path: '/about',
                builder: (context, state) => const AboutPage(),  // πŸ‘ˆ using builder
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
            routerConfig: _router,   // πŸ‘ˆ connect router
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/about');   // πŸ‘ˆ navigate
                    },
                    child: const Text('Go to About'),
                ),
            ),
        );
    }
}

class AboutPage extends StatelessWidget {
    const AboutPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('About')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/');        // πŸ‘ˆ back home
                    },
                    child: const Text('Go Back Home'),
                ),
            ),
        );
    }
}

βœ… Explanation (Very Easy)

πŸ”Ή builder: (context, state) => Widget

The builder returns the Widget you want to show for this route.

πŸ”Ή Why use builder?

  • Easy

  • Clean

  • Best for simple static pages

  • No extra animation or advanced options


βœ…4.pageBuilder:#

Here is the cleanest, simplest, no-error example that demonstrates the pageBuilder: parameter in GoRoute using MaterialApp.router().

This example returns a custom transition page, which is what pageBuilder: is normally used for.

βœ… Simple Program β€” Using pageBuilder: in GoRoute with MaterialApp.router()

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // -------------------------------------
    // GoRouter using pageBuilder:
    // -------------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',
                pageBuilder: (context, state) {
                return MaterialPage(
                    child: const HomePage(),
                );
                },
            ),
            GoRoute(
                path: '/about',
                pageBuilder: (context, state) {
                    return CustomTransitionPage(
                        child: const AboutPage(),
                        transitionsBuilder: (context, animation, secondaryAnimation, child) {
                            return FadeTransition(
                                opacity: animation,
                                child: child,
                            );
                        },
                    );
                },
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
        routerConfig: _router,
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/about');
                    },
                    child: const Text('Go to About (Fade Transition)'),
                ),
            ),
        );
    }
}

class AboutPage extends StatelessWidget {
    const AboutPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('About')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        context.go('/');
                    },
                    child: const Text('Go Back Home'),
                ),
            ),
        );
    }
}

βœ… Explanation (Very Easy)

πŸ”Ή pageBuilder:

This returns a Page object, not a widget.

You can return:

  • MaterialPage

  • CupertinoPage

  • CustomTransitionPage

  • NoTransitionPage

πŸ”Ή Why use pageBuilder?

  • Add animations (fade, slide, scale, rotate)

  • Use platform-specific pages (iOS/Android)

  • More control than builder:

πŸ”Ή In this example:

  • / uses a simple MaterialPage

  • /about uses a CustomTransitionPage with fade animation


βœ…5.routes:#

Here is the simplest possible example **that demonstrates the **routes: parameter in GoRoute using MaterialApp.router().

This example shows:

βœ” parent route

βœ” child routes (nested)

βœ” using routes: inside a GoRoute

βœ… Simple Program β€” Using routes: in GoRoute with MaterialApp.router()

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // ------------------------------------------------
    // GoRouter using routes: (nested child routes)
    // ------------------------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',
                builder: (context, state) => const HomePage(),

                // πŸ‘‡ Child routes under "/"
                routes: [
                    GoRoute(
                        path: 'about',                    // full path becomes: /about
                        builder: (context, state) => const AboutPage(),
                    ),
                    GoRoute(
                        path: 'contact',                  // full path becomes: /contact
                        builder: (context, state) => const ContactPage(),
                    ),
                ],
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
            routerConfig: _router,
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                    ElevatedButton(
                        onPressed: () => context.go('/about'),
                        child: const Text('Go to About'),
                    ),
                    ElevatedButton(
                        onPressed: () => context.go('/contact'),
                        child: const Text('Go to Contact'),
                    ),
                ],
            ),
        );
    }
}

class AboutPage extends StatelessWidget {
    const AboutPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('About')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () => context.go('/'),
                    child: const Text('Go Home'),
                ),
            ),
        );
    }
}

class ContactPage extends StatelessWidget {
    const ContactPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Contact')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () => context.go('/'),
                    child: const Text('Go Home'),
                ),
            ),
        );
    }
}

βœ… Explanation (Very Easy)

πŸ”Ή routes: in GoRoute

The routes: parameter defines child routes (nested routes).

πŸ”Ή Structure in this program

,, code-block:: bash

/ β†’ HomePage /about β†’ AboutPage (child) /contact β†’ ContactPage (child)

πŸ”Ή Why use child routes?

  • Organize large apps easily

  • Create nested pages under a parent

  • Cleaner URL structure


βœ…6.redirect:#

Here is a very simple, clean, beginner-friendly program demonstrating the redirect: parameter in GoRoute using MaterialApp.router().

πŸ‘‰ No authentication logic

πŸ‘‰ No complex code

πŸ‘‰ Only the basic redirect feature

βœ… Simple Program β€” Using redirect: in GoRoute with MaterialApp.router()

This example redirects:

  • If user tries to go to /profile,

they get redirected to /login.

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    MyApp({super.key});

    // ---------------------------------------
    // GoRouter using redirect:
    // ---------------------------------------
    final GoRouter _router = GoRouter(
        routes: [
            GoRoute(
                path: '/',
                builder: (context, state) => const HomePage(),
            ),
            GoRoute(
                path: '/login',
                builder: (context, state) => const LoginPage(),
            ),
            GoRoute(
                path: '/profile',
                builder: (context, state) => const ProfilePage(),

                // πŸ‘‡ redirect rule
                redirect: (context, state) {
                    bool isLoggedIn = false;      // πŸ‘ˆ for demo only
                    if (!isLoggedIn) {
                        return '/login';            // redirect to login page
                    }
                    return null;                  // allow navigation
                },
            ),
        ],
    );

    @override
    Widget build(BuildContext context) {
        return MaterialApp.router(
        routerConfig: _router,
        );
    }
}

class HomePage extends StatelessWidget {
    const HomePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Home')),
            body: Center(
                    child: ElevatedButton(
                    onPressed: () => context.go('/profile'),
                    child: const Text('Go to Profile'),
                ),
            ),
        );
    }
}

class LoginPage extends StatelessWidget {
    const LoginPage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Login')),
            body: Center(
                child: ElevatedButton(
                    onPressed: () => context.go('/'),
                    child: const Text('Back to Home'),
                ),
            ),
        );
    }
}

class ProfilePage extends StatelessWidget {
    const ProfilePage({super.key});

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text('Profile')),
            body: const Center(
                child: Text('Welcome to your profile!'),
            ),
        );
    }
}

βœ… Explanation (Very Easy)

πŸ”Ή redirect: (context, state) { … }

This function decides:

β€’ Should the user stay on this page?

β€’ Or should they be redirected to another page?

πŸ”Ή In our example:

bool isLoggedIn = false;

ince the user is β€œnot logged in,” going to /profile automatically sends them to:

/login

πŸ”Ή return null;

Means β€œno redirect β†’ allow user to enter the page.”


βœ…7.onExit:#

βœ…8.parentNavigatorKey:#