#30DaysMasterFlutter
Learn the basics of Flutter and Dart in 30 days! We provide a roadmap, resources, and project ideas to help you along your journey.
back to 30 Days to Master FlutterDay 19: Navigator 2.0 and the Go Router
Learn about Navigator 2.0 (Flutter’s Router API), the declarative way of handling navigation in Flutter that supports deep linking and the browser URL on the web. Also learn about the Go Router, a package maintained by the Flutter team that is built on the Router API and provides a simple, URL-based approach to routing in Flutter apps.
Project - Quotes App 2.0
Copy and update the quotes app from day 18 to use Go Router.
By the end of this day you will know how to use Navigator 2.0 and the Go Router effectively in your Flutter app and provide a seamless and intuitive user experience.
Make sure to review the basics of Navigation from day 18 before diving into Navigator 2.0 and the Go Router.
Learn about declarative routing: Navigator 2.0 and the Go Router are both based on the declarative routing approach, which simplifies the process of navigating between screens in your app.
Understand the differences between using the Router API directly and the Go Router: the Router API is powerful but low-level and verbose, while go_router wraps it in a simple list of routes. For most apps, go_router is the recommended choice.
Add the package with flutter pub add go_router.
Example implementing basic navigation with the Go Router
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
routes: [
GoRoute(
path: 'quotes/:id',
builder: (context, state) =>
QuoteDetailPage(id: state.pathParameters['id']!),
),
],
),
],
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'MyApp',
routerConfig: _router,
);
}
} BuildContext:context.go('/quotes/42'); // replace the current location
context.push('/quotes/42'); // push a page on top of the current one
context.pop(); // go back Learn the basics of Flutter and Dart in 30 days! We provide a roadmap, resources, and project ideas to help you along your journey.
back to 30 Days to Master FlutterJoin our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #30DaysMasterFlutter challenge. Join now and get started on your journey to mastering Dart and Flutter!