deeplinkHandlerSample function

String deeplinkHandlerSample()

Sample for deeplink_handler.dart utility class

Implementation

String deeplinkHandlerSample() {
  return '''import 'package:app_links/app_links.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class DeeplinkHandler {
  static final DeeplinkHandler _instance = DeeplinkHandler._internal();
  factory DeeplinkHandler() => _instance;
  DeeplinkHandler._internal();

  final AppLinks _appLinks = AppLinks();
  Function(Uri)? _onLinkReceived;

  /// Initialize deep link handling
  /// Primary method: Uses platformDispatcher.defaultRouteName for initial deep links (iOS workaround)
  /// Secondary: Uses app_links for runtime deep links while app is running
  Future<void> initialize({required Function(Uri) onLinkReceived}) async {
    _onLinkReceived = onLinkReceived;

    // Handle initial link if app was opened from a deep link
    // This will be called after the first frame is rendered
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        // PRIMARY METHOD: Get the initial URI from platform dispatcher
        // This is the main solution for iOS with FlutterDeepLinkingEnabled=false
        final initialRoute = WidgetsBinding.instance.platformDispatcher.defaultRouteName;

        if (kDebugMode) {
          print('Platform dispatcher initial route: \$initialRoute');
        }

        if (initialRoute != '/' && initialRoute.isNotEmpty) {
          try {
            final uri = Uri.parse(initialRoute);
            if (kDebugMode) {
              print('✅ Initial deep link detected from platform dispatcher: \$uri');
            }
            _onLinkReceived?.call(uri);
          } catch (parseError) {
            if (kDebugMode) {
              print('❌ Error parsing initial route: \$parseError');
            }
          }
        } else {
          if (kDebugMode) {
            print('ℹ️ No initial deep link detected (normal app launch)');
          }
        }
      } catch (e) {
        if (kDebugMode) {
          print('❌ Error getting initial link from platform dispatcher: \$e');
        }
      }
    });

    // Listen for subsequent deep links while app is running
    // This handles new deep links that arrive after the app is already open
    _appLinks.uriLinkStream.listen(
      (uri) {
        if (kDebugMode) {
          print('🔗 Runtime deep link received: \$uri');
        }
        _onLinkReceived?.call(uri);
      },
      onError: (err) {
        if (kDebugMode) {
          print('❌ Error listening to deep links stream: \$err');
        }
      },
    );
  }

  /// Handle deep link navigation
  void handleDeepLink(Uri uri) {
    if (kDebugMode) {
      print('Handling deep link: \$uri');
      print('Scheme: \${uri.scheme}');
      print('Host: \${uri.host}');
      print('Path: \${uri.path}');
      print('Query Parameters: \${uri.queryParameters}');
    }

    // Your custom deep link handling logic here
    // Example: Navigate to specific screens based on path
    _onLinkReceived?.call(uri);
  }
}
''';
}