Bird
Raised Fist0
Fluttermobile~20 mins

GoRouter package in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Simple Navigation with GoRouter
This screen demonstrates basic navigation between two pages using the GoRouter package in Flutter. The user can navigate from HomePage to DetailsPage and back.
Target UI
HomePage
+---------------------+
| Welcome to HomePage |
|                     |
| [Go to Details]     |
+---------------------+

DetailsPage
+---------------------+
| Details Page        |
|                     |
| [Back to Home]      |
+---------------------+
Use GoRouter for navigation
HomePage with a button labeled 'Go to Details' that navigates to DetailsPage
DetailsPage with a button labeled 'Back to Home' that navigates back to HomePage
Use MaterialApp.router with GoRouter configuration
Starter Code
Flutter
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: Initialize GoRouter and return MaterialApp.router
    return MaterialApp();
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('HomePage')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // TODO: Navigate to DetailsPage
          },
          child: Text('Go to Details'),
        ),
      ),
    );
  }
}

class DetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('DetailsPage')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // TODO: Navigate back to HomePage
          },
          child: Text('Back to Home'),
        ),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Task 4
Solution
Flutter
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

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

final GoRouter _router = GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => HomePage(),
    ),
    GoRoute(
      path: '/details',
      builder: (context, state) => DetailsPage(),
    ),
  ],
);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
      title: 'GoRouter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('HomePage')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            context.go('/details');
          },
          child: Text('Go to Details'),
        ),
      ),
    );
  }
}

class DetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('DetailsPage')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            context.go('/');
          },
          child: Text('Back to Home'),
        ),
      ),
    );
  }
}

We created a GoRouter instance with two routes: '/' for HomePage and '/details' for DetailsPage. The MaterialApp.router uses this router configuration to handle navigation.

In HomePage, pressing the button calls context.go('/details') to navigate to the details page. In DetailsPage, pressing the button calls context.go('/') to go back to the home page.

This setup uses GoRouter's simple API to manage navigation declaratively and cleanly.

Final Result
Completed Screen
HomePage
+---------------------+
| Welcome to HomePage |
|                     |
| [Go to Details]     |
+---------------------+

DetailsPage
+---------------------+
| Details Page        |
|                     |
| [Back to Home]      |
+---------------------+
Tap 'Go to Details' button on HomePage navigates to DetailsPage
Tap 'Back to Home' button on DetailsPage navigates back to HomePage
Stretch Goal
Add a third page called AboutPage accessible from HomePage with a button labeled 'About'.
💡 Hint
Add a new route '/about' in GoRouter and a button in HomePage that calls context.go('/about'). Create AboutPage widget with a back button.

Practice

(1/5)
1. What is the main purpose of the GoRouter package in Flutter?
easy
A. To handle database connections
B. To manage state across widgets
C. To style widgets with custom themes
D. To simplify and manage screen navigation in Flutter apps

Solution

  1. Step 1: Understand GoRouter's role

    GoRouter is designed to make navigation between screens easier and clearer in Flutter apps.
  2. Step 2: Compare with other options

    Options A, C, and D relate to database, styling, and state management, which are not GoRouter's purpose.
  3. Final Answer:

    To simplify and manage screen navigation in Flutter apps -> Option D
  4. Quick Check:

    GoRouter = Navigation helper [OK]
Hint: Remember GoRouter is about moving between screens [OK]
Common Mistakes:
  • Confusing navigation with state management
  • Thinking GoRouter styles widgets
  • Assuming GoRouter handles databases
2. Which of the following is the correct way to navigate to a new route named '/profile' using GoRouter?
easy
A. context.navigateTo('/profile');
B. Navigator.push('/profile');
C. context.go('/profile');
D. goRouter.push('/profile');

Solution

  1. Step 1: Recall GoRouter navigation syntax

    GoRouter uses context.go(path) to navigate to a route.
  2. Step 2: Check each option

    context.go('/profile'); uses context.go('/profile'), which is correct. Options B, C, and D use incorrect methods or objects.
  3. Final Answer:

    context.go('/profile'); -> Option C
  4. Quick Check:

    Use context.go() for navigation [OK]
Hint: Use context.go() with the route path to navigate [OK]
Common Mistakes:
  • Using Navigator.push instead of context.go
  • Calling non-existent methods like context.navigateTo
  • Trying to call goRouter.push without context
3. Given this GoRouter setup:
final router = GoRouter(
  routes: [
    GoRoute(path: '/', builder: (context, state) => HomeScreen()),
    GoRoute(path: '/settings', builder: (context, state) => SettingsScreen()),
  ],
);

// In HomeScreen's button onPressed:
context.go('/settings');
What screen will be displayed after pressing the button?
medium
A. HomeScreen
B. SettingsScreen
C. An error screen
D. A blank screen

Solution

  1. Step 1: Analyze the routes defined

    There are two routes: '/' for HomeScreen and '/settings' for SettingsScreen.
  2. Step 2: Understand the navigation call

    The button calls context.go('/settings'), which navigates to the '/settings' route.
  3. Final Answer:

    SettingsScreen -> Option B
  4. Quick Check:

    context.go('/settings') shows SettingsScreen [OK]
Hint: context.go(path) shows the screen for that path [OK]
Common Mistakes:
  • Assuming context.go('/') stays on HomeScreen
  • Thinking navigation causes error without extra setup
  • Confusing builder with route path
4. What is wrong with this GoRouter route definition?
GoRoute(
  path: '/profile',
  builder: (context) => ProfileScreen(),
)
medium
A. The builder function is missing the second parameter 'state'
B. The path should start with a capital letter
C. ProfileScreen() must be wrapped in a MaterialApp
D. GoRoute requires a name property

Solution

  1. Step 1: Check GoRoute builder signature

    The builder function must accept two parameters: context and state.
  2. Step 2: Identify the error

    The code only has one parameter (context), missing the required 'state' parameter.
  3. Final Answer:

    The builder function is missing the second parameter 'state' -> Option A
  4. Quick Check:

    builder(context, state) needs two parameters [OK]
Hint: Builder always needs (context, state) parameters [OK]
Common Mistakes:
  • Omitting the 'state' parameter in builder
  • Thinking path must be capitalized
  • Assuming name property is mandatory
  • Wrapping screen in MaterialApp inside builder
5. You want to pass a user ID as a parameter in the route path using GoRouter. Which route definition and navigation call are correct?
// Route definition options:
A) GoRoute(path: '/user/:id', builder: (context, state) => UserScreen(id: state.params['id']!))
B) GoRoute(path: '/user', builder: (context, state) => UserScreen(id: state.params['id']!))

// Navigation call options:
1) context.go('/user/123');
2) context.go('/user?id=123');
hard
A. Use route A with navigation call 1
B. Use route A with navigation call 2
C. Use route B with navigation call 1
D. Use route B with navigation call 2

Solution

  1. Step 1: Understand route parameter syntax

    Route path with ':id' means the ID is part of the path, so '/user/:id' is correct for path parameters.
  2. Step 2: Match navigation call to route

    To pass the ID in the path, navigation must be like '/user/123', so call 1 matches route A.
  3. Final Answer:

    Use route A with navigation call 1 -> Option A
  4. Quick Check:

    Path param ':id' needs '/user/123' navigation [OK]
Hint: Use ':param' in path and '/value' in go() call [OK]
Common Mistakes:
  • Using query parameters with path param syntax
  • Missing ':id' in route path
  • Passing ID as query string when route expects path param
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard