teleport_router_generator
Code generator for teleport_router - A Flutter package that provides type-safe, annotation-based routing built on top of go_router.
Overview
teleport_router_generator processes @TeleportRoute, @TeleportShellRoute, and @TeleportStatefulShellRoute annotations to automatically generate type-safe routing code. It eliminates boilerplate and ensures compile-time safety for your navigation logic.
Installation
Add to your dev_dependencies in pubspec.yaml:
dependencies:
teleport_router: any
dev_dependencies:
build_runner: any
teleport_router_generator: any
Usage
1. Annotate Your Pages
import 'package:teleport_router_annotation/teleport_router_annotation.dart';
@TeleportRoute()
class HomePage extends StatelessWidget {
const HomePage({super.key});
// ... widget implementation
}
2. Run Code Generation
# One-time generation
flutter pub run build_runner build
# Watch mode (auto-regenerate on changes)
flutter pub run build_runner watch
# Delete conflicting outputs and rebuild
flutter pub run build_runner build --delete-conflicting-outputs
3. Use Generated Routes
After generation, you'll get type-safe route classes:
// Navigate using generated route
HomeRoute().teleport(context);
// With parameters
UserRoute(userId: '123').teleport(context);
Generated Code
The generator creates:
- Type-safe route classes: One for each
@TeleportRouteannotated page - Parameter extraction: Automatic handling of path, query, and extra parameters
- Route configuration:
TeleportRouteInfoobjects for go_router integration - Global route list:
teleportRoutescontaining all routes in your app - Static
fromDatamethod: Reconstruct typed route instances from generic data objects - Class-based Callbacks: Support for
TeleportRedirectandTeleportOnExitimplementations
Configuration
Customize the output file location in build.yaml:
targets:
$default:
builders:
teleport_router_generator:
options:
output: lib/generated/routes.g.dart
Features
- ✅ Generates type-safe route classes (e.g.,
HomeRoute,UserRoute) - ✅ Handles parameter extraction from path, query, and extra data
- ✅ Supports custom transitions and global configuration
- ✅ Supports shell routes (
TeleportShellRoute,TeleportStatefulShellRoute) - ✅ Compile-time validation of route parameters
- ✅ Static reconstruction via
fromDatamethod - ✅ Class-based redirection and lifecycle guards
- ✅ Automatic path generation from class names
Related Packages
- teleport_router - Main routing package
- teleport_router_annotation - Annotation definitions
Example
See the teleport_router example for a complete working application demonstrating all features.
License
See the main teleport_router repository for license information.
Libraries
- teleport_router_generator
- Code generation library for teleport_router.