Flutter Navigation vs. Go Router: Understanding the Differences
Abdelaty Abdallah
Senior Software Developer @ _VOIS | Mobile Application Development
In the ever-evolving landscape of mobile app development, Flutter has emerged as a powerful framework for building natively compiled applications for mobile, web, and desktop from a single codebase. Among the many aspects of Flutter development, navigation is a critical one, as it dictates how users move through different parts of an app. Two popular approaches to handling navigation in Flutter are the built-in navigation system and the Go Router package. In this article, we will explore the differences between these two methods, examine their performance, and highlight their pros and cons with examples.
Flutter's Built-In Navigation
Overview
Flutter's built-in navigation system is based on the Navigator and Route classes. It uses a stack-based approach, where routes (screens or pages) are pushed onto or popped off the stack.
Example
Here's a simple example of Flutter's built-in navigation:
// Main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Navigation Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
child: Text('Go to Second Page'),
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back to Home Page'),
),
),
);
}
}
Deep Linking with Built-In Navigation
To handle deep linking with Flutter's built-in navigation, you can use the onGenerateRoute method and Uri parsing. Here's an example:
// Main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Deep Linking Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
onGenerateRoute: (settings) {
final Uri uri = Uri.parse(settings.name ?? '/');
if (uri.pathSegments.length == 2 && uri.pathSegments.first == 'second') {
final id = uri.pathSegments[1];
return MaterialPageRoute(builder: (context) => SecondPage(id: id));
}
return MaterialPageRoute(builder: (context) => HomePage());
},
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second/123');
},
child: Text('Go to Second Page with ID 123'),
),
),
);
}
}
class SecondPage extends StatelessWidget {
final String id;
SecondPage({required this.id});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Text('Page ID: $id'),
),
);
}
}
Pros
Cons
Go Router
Overview
Go Router is a third-party package that simplifies navigation in Flutter apps. It offers a declarative approach to defining routes and handles deep linking and URL-based navigation more effectively.
Example
Here's an example of navigation using Go Router:
//Main.dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final GoRouter _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => HomePage(),
),
GoRoute(
path: '/second/:id',
builder: (context, state) {
final id = state.params['id'];
return SecondPage(id: id!);
},
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
title: 'Go Router Navigation Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
context.go('/second/123');
},
child: Text('Go to Second Page with ID 123'),
),
),
);
}
}
class SecondPage extends StatelessWidget {
final String id;
SecondPage({required this.id});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Text('Page ID: $id'),
),
);
}
}
领英推荐
Deep Linking with Go Router
Go Router simplifies deep linking significantly. It allows for a declarative definition of routes and handles URL parsing internally. The above example demonstrates how to navigate to a route with a parameter using deep linking.
Pros
Cons
Performance Comparison
Performance is a crucial consideration in choosing a navigation approach. Both Flutter's built-in navigation and Go Router offer efficient navigation mechanisms, but there are some differences:
- Overhead: Minimal overhead since it's part of the Flutter framework.
- Memory Usage: Efficient memory usage due to its stack-based .
- Initialization: Slightly more overhead due to additional package and initial setup.
- Efficiency: Highly efficient for handling complex routing, deep linking, and URL-based navigation.
In practice, the performance differences are often negligible for most applications. The choice between the two should primarily be based on the complexity of the app’s navigation requirements and the developer’s preference for a declarative versus imperative style.
Personally Preferred: Go Router
As a developer, my personal preference leans towards Go Router. Here are a few reasons why:
While Flutter's built-in navigation is powerful and sufficient for many applications, Go Router's additional features and ease of use make it a preferred choice for projects that demand advanced routing capabilities.
Conclusion
Choosing the right navigation approach in Flutter depends on the specific needs of your application. Flutter's built-in navigation is straightforward and well-integrated, making it suitable for simpler apps or those with basic navigation requirements. On the other hand, Go Router offers powerful features for handling more complex routing, deep linking, and web URL navigation, making it a strong choice for more advanced applications.
Both approaches have their strengths and weaknesses, and understanding these can help you make an informed decision that best suits your project’s requirements. Happy coding!
Backend Developer (Django)
1 周Go router is not a third party navigator, it is also maintained by flutter team