Skip to main content
Utility functions for working with deeplinks in NavigationUtils.

Functions

openDeeplinkDestination

static bool openDeeplinkDestination({
  required Uri? uri,
  required List<DeeplinkDestination> deeplinkDestinations,
  required BaseRouterDelegate routerDelegate,
  DeeplinkDestination? deeplinkDestination,
  bool authenticated = true,
  DefaultRoute? currentRoute,
  List<String> excludeDeeplinkNavigationPages = const [],
  bool push = false
})
Opens a deeplink destination if conditions are met. This function is responsible for determining if a DeeplinkDestination can be opened (using the canOpenDeeplinkDestination function) and if so, it performs the necessary navigation.
uri
Uri?
required
The URI for the deeplink.
List of all available DeeplinkDestination instances.
routerDelegate
BaseRouterDelegate
required
The router delegate currently handling app navigation.
The DeeplinkDestination object to be opened.
authenticated
bool
default:"true"
Whether the user is authenticated.
currentRoute
DefaultRoute?
The currently active route.
List of routes from which deeplink navigation is not permitted.
push
bool
default:"false"
If true, the destination is pushed onto the navigation stack.
Returns true if the deeplink was successfully opened, false otherwise.

canOpenDeeplinkDestination

static bool canOpenDeeplinkDestination({
  required Uri? uri,
  required List<DeeplinkDestination> deeplinkDestinations,
  required BaseRouterDelegate routerDelegate,
  DeeplinkDestination? deeplinkDestination,
  Map<String, String> pathParameters = const {},
  bool authenticated = true,
  DefaultRoute? currentRoute,
  List<String> excludeDeeplinkNavigationPages = const []
})
Determines whether a DeeplinkDestination can be opened. The method checks various conditions, like whether the URI is null, authentication is needed, whether navigation from the current page is allowed, etc., and returns true if the DeeplinkDestination can be opened.
uri
Uri?
required
The URI for the deeplink.
List of all available DeeplinkDestination instances.
routerDelegate
BaseRouterDelegate
required
The router delegate currently managing app navigation.
The specific destination to be checked.
pathParameters
Map<String, String>
default:"{}"
Path parameters for the destination.
authenticated
bool
default:"true"
Whether the user is authenticated.
currentRoute
DefaultRoute?
The currently active route.
List of routes from which deeplink navigation is not allowed.
Returns true if the deeplink can be opened, false otherwise.

getDeeplinkDestinationFromUri

static DeeplinkDestination? getDeeplinkDestinationFromUri(
  List<DeeplinkDestination> deeplinkDestinations,
  Uri? uri
)
Gets the DeeplinkDestination from a URI. The function first attempts to find a direct path match. If no match is found, it then attempts to match the path pattern.
List of available deeplink destinations.
uri
Uri?
required
The URI to match.
Returns the matching DeeplinkDestination or null if no match is found.

getDeeplinkDestinationFromUrl

static DeeplinkDestination? getDeeplinkDestinationFromUrl(
  List<DeeplinkDestination> deeplinkDestinations,
  String? url
)
Gets the DeeplinkDestination from a URL string.
List of available deeplink destinations.
url
String?
required
The URL to match.
Returns the matching DeeplinkDestination or null if no match is found.

Example

// Define deeplink destinations
final deeplinkDestinations = [
  DeeplinkDestination(
    deeplinkUrl: '/product/:id',
    destinationLabel: 'product-detail',
    authenticationRequired: false,
  ),
  DeeplinkDestination(
    deeplinkUrl: '/account/settings',
    destinationLabel: 'settings',
    authenticationRequired: true,
  ),
];

// Open a deeplink
final uri = Uri.parse('myapp://product/123');
final success = NavigationUtils.openDeeplinkDestination(
  uri: uri,
  deeplinkDestinations: deeplinkDestinations,
  routerDelegate: NavigationManager.instance.routerDelegate,
  authenticated: true,
);

if (success) {
  print('Deeplink opened successfully');
}

// Check if a deeplink can be opened
final canOpen = NavigationUtils.canOpenDeeplinkDestination(
  uri: uri,
  deeplinkDestinations: deeplinkDestinations,
  routerDelegate: NavigationManager.instance.routerDelegate,
  authenticated: false,
);

if (!canOpen) {
  print('Deeplink requires authentication');
}

// Get deeplink destination from URI
final destination = NavigationUtils.getDeeplinkDestinationFromUri(
  deeplinkDestinations,
  uri,
);

if (destination != null) {
  print('Found destination: ${destination.destinationLabel}');
}

Build docs developers (and LLMs) love