Configure GoRouter for declarative navigation
✓Works with OpenClaudeYou are a Flutter developer. The user wants to configure GoRouter for declarative navigation in a Flutter app.
What to check first
- Verify
go_routerpackage is added topubspec.yaml(version 10.0.0 or higher recommended) - Run
flutter pub getto ensure dependencies are installed - Check that your app uses
MaterialApp.routerinstead ofMaterialApp
Steps
- Add
go_routerto pubspec.yaml and runflutter pub get - Create a GoRouter instance with
GoRoutedefinitions in your main.dart or separate router file - Define routes with
path,name, andbuilderproperties for each screen - Wrap your MaterialApp with
routerConfigparameter pointing to your GoRouter instance - Replace all Navigator.push calls with
context.go()orcontext.push()for navigation - Define error pages using the
errorBuilderproperty in GoRouter - Add route guards with
redirectcallbacks to enforce authentication or authorization flows - Test deep linking by passing route paths to
context.go()with query parameters using URI syntax
Code
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
// Define your screens
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/details/1'),
child: const Text('Go to Details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
final String id;
const DetailsScreen({Key? key, required this.id}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: Center(child: Text('Item ID: $id')),
);
}
}
class ErrorScreen extends StatelessWidget {
final GoException exception;
const ErrorScreen({Key? key, required this.exception}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Error')),
body: Center(child: Text('Error: ${exception.error}')),
);
}
}
// Create GoRouter
final router = GoRouter(
initialLocation: '/',
redirect: (context, state) {
// Add authentication checks here
return null; // Return null to proceed normally
},
routes: [
GoRoute(
path: '/',
name: 'home',
builder: (context, state)
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Flutter Skills
Other Claude Code skills in the same category — free to download.
Flutter Widget
Build custom Flutter widgets with state management
Flutter Riverpod
Set up Riverpod for state management in Flutter
Flutter HTTP
Build HTTP client with Dio and interceptors
Flutter Firebase
Integrate Firebase (Auth, Firestore, Storage) in Flutter
Flutter Testing
Write widget tests and integration tests for Flutter
Flutter State Management with Riverpod
Manage app state in Flutter using Riverpod 2.x for type-safe reactive state
Flutter Platform Channels
Call native iOS and Android code from Flutter via platform channels
Want a Flutter skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.