go_router: URL-first navigation for Flutter
go_router is useful because it focuses on declarative routing, deep links, redirects, and ShellRoute. The important engineering move is to place the package behind a clear boundary, so your Flutter UI depends on a stable capability rather than a vendor-shaped API.
What the library should own
- Keep package calls inside a named adapter or feature boundary.
- Make lifecycle, errors, and loading state part of a testable contract.
- Expose only the capability the app needs; hide implementation details from the whole tree.
A focused starting point
final router = GoRouter(
redirect: (context, state) => isSignedIn || state.uri.path == '/login'
? null
: '/login',
routes: [
GoRoute(path: '/', builder: (_, __) => const HomePage()),
GoRoute(path: '/orders/:id', builder: (_, state) =>
OrderPage(id: state.pathParameters['id']!)),
],
);
Production checklist
- Read the README and changelog for the exact version you pin.
- Add one test for lifecycle, failure, and app background/foreground behavior.
- Verify every platform your product supports, not only the developer machine.
- Record ownership, upgrade cadence, and rollback notes in the repository.
Common pitfall
Do not hide auth redirects inside widgets; they belong at the route boundary.
Takeaway
A good open-source library does not replace architecture. It makes one difficult boundary clearer, observable, and easier to replace.
Originally published on FlutterCook. Read the latest version there — that copy is the one kept up to date.
Nhận xét
Đăng nhận xét