initialize static method

Future<void> initialize({
  1. DeepLinkHandler? onLink,
  2. Map<String, String> routeMappings = const {},
})

Initializes deep link listening across cold-start and runtime event channels.

Optionally configures a custom onLink callback handler and routeMappings table.

Example:

await BloomDeepLinks.initialize(
  routeMappings: {'help': '/support'},
);

Implementation

static Future<void> initialize({
  DeepLinkHandler? onLink,
  Map<String, String> routeMappings = const {},
}) async {
  _customHandler = onLink;
  _routeMappings = routeMappings;

  // 1. Get initial launch deep link (cold start)
  try {
    final initialLinkStr = await _channel.invokeMethod<String>('getInitialLink');
    if (initialLinkStr != null && initialLinkStr.isNotEmpty) {
      final uri = Uri.tryParse(initialLinkStr);
      if (uri != null) {
        logger.info('BloomDeepLinks: App cold-started with deep link: $uri');
        _dispatch(uri);
      }
    }
  } catch (_) {}

  // 2. Listen to incoming foreground/background links
  try {
    _linkSubscription = _eventChannel.receiveBroadcastStream().listen(
      (dynamic link) {
        if (link is String && link.isNotEmpty) {
          final uri = Uri.tryParse(link);
          if (uri != null) {
            logger.info('BloomDeepLinks: Received runtime deep link: $uri');
            _dispatch(uri);
          }
        }
      },
      onError: (err) {
        logger.warn('BloomDeepLinks: Event channel error: $err');
      },
    );
  } catch (_) {}
}