3.2 KiB
3.2 KiB
GoRouter Quick Reference
Import
import 'package:minhthu/core/router/app_router.dart';
Navigation Commands
Basic Navigation
// Login page
context.goToLogin();
// Warehouses list
context.goToWarehouses();
// Operations (requires warehouse)
context.goToOperations(warehouse);
// Products (requires warehouse and operation type)
context.goToProducts(
warehouse: warehouse,
operationType: 'import', // or 'export'
);
// Go back
context.goBack();
Named Routes (Alternative)
context.goToLoginNamed();
context.goToWarehousesNamed();
context.goToOperationsNamed(warehouse);
context.goToProductsNamed(
warehouse: warehouse,
operationType: 'export',
);
Common Usage Patterns
Warehouse Selection → Operations
onTap: () {
context.goToOperations(warehouse);
}
Operation Selection → Products
// Import
onTap: () {
context.goToProducts(
warehouse: warehouse,
operationType: 'import',
);
}
// Export
onTap: () {
context.goToProducts(
warehouse: warehouse,
operationType: 'export',
);
}
Logout
IconButton(
icon: const Icon(Icons.logout),
onPressed: () async {
await ref.read(authProvider.notifier).logout();
// Router auto-redirects to /login
},
)
Route Paths
| Path | Name | Description |
|---|---|---|
/login |
login |
Login page |
/warehouses |
warehouses |
Warehouse list (protected) |
/operations |
operations |
Operation selection (protected) |
/products |
products |
Product list (protected) |
Authentication
Check Status
final isAuth = await SecureStorage().isAuthenticated();
Auto-Redirect Rules
- Not authenticated →
/login - Authenticated on
/login→/warehouses - Missing parameters → Previous valid page
Error Handling
Missing Parameters
// Automatically redirected to safe page
// Error screen shown briefly
Page Not Found
// Custom 404 page shown
// Can navigate back to login
Complete Example
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:minhthu/core/router/app_router.dart';
class WarehouseCard extends ConsumerWidget {
final WarehouseEntity warehouse;
const WarehouseCard({required this.warehouse});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Card(
child: ListTile(
title: Text(warehouse.name),
subtitle: Text(warehouse.code),
trailing: Icon(Icons.arrow_forward),
onTap: () {
// Navigate to operations
context.goToOperations(warehouse);
},
),
);
}
}
Tips
- Use extension methods - They provide type safety and auto-completion
- Let router handle auth - Don't manually check authentication in pages
- Validate early - Router validates parameters automatically
- Use named routes - For better route management in large apps
See Also
- Full documentation:
/lib/core/router/README.md - Setup guide:
/ROUTER_SETUP.md - Examples:
/lib/features/warehouse/presentation/pages/warehouse_selection_page_example.dart