Navigation and Routing
Flutter navigation manages a stack of routes. Navigator 1.0 uses imperative push/pop. go_router provides declarative URL-based routing.
Basic Navigation
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailPage()),
);
Navigator.pop(context);
Passing Data
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(item: selectedItem),
),
);
Named Routes
MaterialApp(
routes: {
'/': (context) => HomePage(),
'/detail': (context) => DetailPage(),
},
);
Navigator.pushNamed(context, '/detail');
go_router
final router = GoRouter(
routes: [
GoRoute(path: '/', builder: (_, __) => HomePage()),
GoRoute(path: '/detail/:id', builder: (_, state) {
final id = state.pathParameters['id']!;
return DetailPage(id: id);
}),
],
);
Bottom Navigation
BottomNavigationBar(
currentIndex: _index,
onTap: (i) => setState(() => _index = i),
items: [/* tabs */],
)
Common Pitfalls
- Ignoring null safety warnings and using
!operator carelessly. - Calling setState excessively causing unnecessary rebuilds.
- Mixing async patterns without proper error handling.
- Hardcoding values that should be theme constants.
Best Practices
- Prefer const constructors for static widgets.
- Use named parameters for widget constructors.
- Extract reusable widgets into separate files.
- Write widget tests for critical UI paths.
Memory and Performance Notes
Route transitions animate on GPU. Preload data before navigation for smooth UX.
Exercise
Build a 3-screen app with go_router: home, list, detail. Pass an ID via URL parameter.
Hint: Use context.go(’/path’) for declarative navigation with go_router.
Summary
Practice with Flutter DevTools open to observe rebuilds and performance.
Debugging Checklist
- Read the full error in the console.
- Use Flutter DevTools widget inspector.
- Hot restart vs hot reload — know the difference.
- Check null safety and type errors first.
- Simplify widget tree to isolate the issue.
Real-World Application
Production Flutter apps combine these patterns with analytics, crash reporting, and CI/CD.
Further Reading
Flutter documentation, Dart language tour, and pub.dev packages.
Additional Examples
Consider how this topic applies in a larger project:
// Break the problem into smaller functions
// Test each function independently
// Integrate incrementally
Working through variations of the examples above builds deeper understanding than reading alone.
Interview and Review Questions
- Explain the core concept of this topic in your own words.
- What happens when this code runs with edge-case input (empty, null, zero, max value)?
- How would you debug a bug related to this topic in production?
- What are the performance implications of the approach shown here?
- How does this feature compare to the equivalent in another language you know?
Related Topics in This Path
Review adjacent pages in the learning path before and after this one. Concepts build on each other — skipping ahead often leads to confusion when later pages assume mastery of earlier material.
Return to the section index if you need to fill gaps in prerequisite knowledge.