flutter_deep_link_router

pub package License: MIT Flutter Dart

A production-ready deep linking and navigation middleware for Flutter. It handles URI parsing, guarded routes, redirect chains, link previews, and cold-start deep link restoration with ease.

Deep linking in Flutter often feels disjointed. handling cold starts correctly, implementing complex authentication guards, and managing redirection chains usually require significant boilerplate. This package provides a centralized, robust middleware layer that sits between your deep links and your navigation logic (supporting both go_router and Navigator 2.0).

Features

  • ✅ Robust URI Parsing: Pattern matching with path parameter support (e.g., /product/:id).
  • ✅ Route Guards: Built-in AuthGuard, RoleGuard, and MaintenanceGuard.
  • ✅ Redirect Chains: Seamlessly handle multiple redirects and conditional redirection.
  • ✅ Cold Start Restoration: Built-in support for capturing and restoring deep links received when the app was closed.
  • ✅ Middleware Layer: Decouple link parsing/validation from UI navigation.
  • ✅ Link Previews: DeepLinkPreview widget for fast, metadata-driven UI previews.
  • ✅ Event Observation: DeepLinkObserver to listen to deep link events across your app.
  • ✅ Framework Compatible: Works with go_router and Navigator 2.0.

Installation

Add this to your pubspec.yaml:

dependencies:
  flutter_deep_link_router: ^1.0.0

Setup

1. Defining Routes

Define your routes using RouteDefinition. You can specify guards, redirects, and metadata.

final routes = [
  RouteDefinition(
    path: '/home',
    builder: (context) => const HomeScreen(),
  ),
  RouteDefinition(
    path: '/profile/:id',
    guards: [AuthGuard(isAuthenticated: checkAuth, redirectPath: '/login')],
    builder: (context) => const ProfileScreen(),
  ),
];

2. Initializing DeepLinkRouter

Initialize the router and provide a NavigatorDelegate to bridge to your actual navigator.

final router = DeepLinkRouter(
  routes: routes,
  delegate: MyNavigatorDelegate(), // Bridge to your navigation logic
  parser: DeepLinkParser(
    schemes: ['https', 'myapp'],
    hosts: ['myapp.com'],
  ),
);

Usage

// Manually handle a URI
router.handleUri('https://myapp.com/product/123');

Cold Start Handling

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  final router = DeepLinkRouter(...);
  
  // Handle cold start link if present
  await router.handleColdStart();
  
  runApp(MyApp(router: router));
}
DeepLinkPreview(
  uri: 'https://myapp.com/product/123',
  router: myRouter,
  builder: (context, metadata) => Card(
    child: Column(
      children: [
        if (metadata?.imageUrl != null) Image.network(metadata!.imageUrl!),
        Text(metadata?.title ?? 'Loading...'),
      ],
    ),
  ),
)

Framework Integration

go_router Integration

final goRouter = router.toGoRouter(); // Generate GoRouter from definitions
// OR use DeepLinkRouter as a handler for existing GoRouter
router.delegate = DeepLinkGoRouterDelegate(myGoRouter);
MaterialApp.router(
  routerDelegate: router.toRouterDelegate(),
  routeInformationParser: router.toRouteInformationParser(),
);

API Reference

  • DeepLink: The core model representing a parsed URI.
  • DeepLinkRouter: Central orchestration class.
  • RouteGuard: Abstract class for navigation middleware.
  • DeepLinkParser: Handles URI pattern matching.
  • DeepLinkObserver: Widget for observing router events.

Contributing

Contributions are welcome! Please see our Contributing Guide for more details.

License

MIT License - Copyright (c) 2025 George Ikwegbu


Developed by George Ikwegbu