home / skills / hoangnguyen0403 / agent-skills-standard / getx-navigation

getx-navigation skill

/skills/flutter/getx-navigation

This skill helps you implement decoupled navigation with GetX, using named routes, bindings, and middleware for robust Flutter apps.

npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill getx-navigation

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
1.5 KB
---
name: Flutter GetX Navigation
description: Context-less navigation, named routes, and middleware using GetX.
metadata:
  labels: [navigation, getx, routing, middleware]
  triggers:
    files: ['**/app_pages.dart', '**/app_routes.dart']
    keywords: [GetPage, Get.to, Get.off, Get.offAll, Get.toNamed, GetMiddleware]
---

# GetX Navigation

## **Priority: P0 (CRITICAL)**

Decoupled navigation system allowing UI transitions without `BuildContext`.

## Guidelines

- **Named Routes**: Use `Get.toNamed('/path')`. Define routes in `AppPages`.
- **Navigation APIs**:
  - `Get.to()`: Push new route.
  - `Get.off()`: Replace current route.
  - `Get.offAll()`: Clear stack and push.
  - `Get.back()`: Pop route/dialog/bottomSheet.
- **Bindings**: Link routes with `Bindings` for automated lifecycle.
- **Middleware**: Implement `GetMiddleware` for Auth/Permission guards.

## Code Example

```dart
static final routes = [
  GetPage(
    name: _Paths.HOME,
    page: () => HomeView(),
    binding: HomeBinding(),
    middlewares: [AuthMiddleware()],
  ),
];

// Usage in Controller
void logout() => Get.offAllNamed(Routes.LOGIN);
```

## Anti-Patterns

- **Navigator Context**: Do not use `Navigator.of(context)` with GetX.
- **Hardcoded Routes**: Use a `Routes` constant class.
- **Direct Dialogs**: Use `Get.dialog()` and `Get.snackbar()`.

## References

- [AppPages Config](references/app-pages.md)
- [Middleware Implementation](references/middleware-example.md)

## Related Topics

getx-state-management | feature-based-clean-architecture

Overview

This skill documents context-less navigation, named routes, and middleware patterns using GetX for Flutter. It focuses on decoupling navigation from BuildContext, using named routes, bindings for lifecycle management, and middleware for guards like authentication. The guidance emphasizes safe, consistent routing and common anti-patterns to avoid.

How this skill works

The skill explains how to register GetPage routes in a central AppPages/Routes file and navigate via Get.toNamed, Get.off, Get.offAll, and Get.back without passing BuildContext. It shows how to attach Bindings to routes so controllers and services are instantiated and disposed automatically. It also covers GetMiddleware implementations for route guards and permission checks that run before navigation.

When to use it

  • When you want navigation decoupled from widget BuildContext and easier testing.
  • When standardizing routes across a large Flutter app with feature modules.
  • When you need automatic controller/service lifecycle management per route via Bindings.
  • When enforcing authentication or permission checks before entering screens.
  • When you want consistent dialog and snack patterns using Get.dialog and Get.snackbar.

Best practices

  • Define all route names in a single Routes constant class to avoid hardcoded strings.
  • Use Get.toNamed / Get.offNamed / Get.offAllNamed for predictable navigation and stack control.
  • Attach Bindings to GetPage entries to create controllers/services on demand and clean up automatically.
  • Implement GetMiddleware for auth checks, redirecting unauthenticated users with Get.offAllNamed.
  • Avoid Navigator.of(context); prefer Get.* APIs to keep code context-less and testable.
  • Use Get.dialog and Get.snackbar instead of direct Material dialog calls for consistency.

Example use cases

  • Feature-based app where each module registers its GetPage routes and bindings centrally.
  • Protecting admin screens with an AuthMiddleware that redirects to the login route.
  • Logging out a user by clearing the navigation stack with Get.offAllNamed(Routes.LOGIN).
  • Showing cross-screen snackbars or dialogs from controllers without passing context.
  • Replacing a login flow with Get.off to remove the login screen from the back stack after success.

FAQ

How do I perform navigation without BuildContext?

Use Get.toNamed, Get.offNamed, Get.offAllNamed, and Get.back from controllers or services; GetX provides global access without BuildContext.

Where should I register route bindings and middleware?

Register Bindings and middleware on each GetPage within your centralized AppPages/Routes configuration so lifecycle and guards run automatically when navigating.