llfbandit / app_links

Android App Links, Deep Links, iOs Universal Links and Custom URL schemes handler for Flutter.
https://pub.dev/packages/app_links
Apache License 2.0
219 stars 80 forks source link

Path issue #133

Closed angwandi closed 5 months ago

angwandi commented 5 months ago

I am using the package with FlowLinks. All working well as expected but once the app opens the path and redirect is not working.

I have a list of events in an event page and event detail page.

My set up is to open the event detail page. But it's only opening the app and not redirecting to the event detail page.

Please help.

This is the link set up on FlowLinks : https://mydomain.com/client-home-page-content-page/events/1f81c20ebbb2429f8f7500b1af84729f

This the AppLink code below :


  WidgetsFlutterBinding.ensureInitialized();

  /// Initialize Firebase
  /// This is necessary for all Firebase services that your app uses
  /// The options parameter specifies which Firebase project the app should connect to
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  FlutterError.onError = (errorDetails) {
    FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
  };

  /// Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  PlatformDispatcher.instance.onError = (error, stack) {
    FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
    return true;
  };

  /// This code activates Firebase App Check for the application.
  /// Firebase App Check helps protect your backend resources from abuse,
  /// such as billing fraud or phishing, by asserting that the traffic
  /// to your app's backend resources is coming from your authentic app.
  /// It uses different providers for web, Android, and Apple platforms.
  await FirebaseAppCheck.instance.activate(
    webProvider: ReCaptchaV3Provider(Env.gcpRecap),
    androidProvider:
        kDebugMode ? AndroidProvider.debug : AndroidProvider.playIntegrity,
    appleProvider: AppleProvider.appAttest,
  );

  /// Configuring Firebase UI Auth providers. Here, Google and Apple are
  /// being used as providers.
  /// For Google, the client ID is being fetched from environment variables.
  /// For Apple, the scope is set to 'email' to fetch the user's
  /// email during authentication.
  FirebaseUIAuth.configureProviders([
    GoogleProvider(clientId: Env.googleClientId),
    apple_auth.AppleProvider(scopes: {'email'}),
  ]);

  /// Set the URL strategy for the web application to use path-based URLs
  setPathUrlStrategy();

  /// Configure GoRouter to reflect the imperative APIs in the URL
  GoRouter.optionURLReflectsImperativeAPIs = true;

  /// Initialize Sentry for error tracking. The DSN is fetched from
  /// environment variables.
  /// The appRunner parameter specifies the
  /// function to execute after Sentry is initialized.
  await SentryFlutter.init(
    (options) {
      options.dsn = Env.sentryDns;
    },
    appRunner: () async {
      runApp(
        const ProviderScope(
          child: MyjijiAdminPortal(),
        ),
      );
    },
  );
}

class MyjijiAdminPortal extends ConsumerStatefulWidget {
  const MyjijiAdminPortal({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _MyjijiAdminPortalState();
}

class _MyjijiAdminPortalState extends ConsumerState<MyjijiAdminPortal> {
  late AppLinks _appLinks;
  StreamSubscription<Uri>? _linkSubscription;
  @override
  void initState() {
    super.initState();
    initDeepLinks();
  }

  @override
  void dispose() {
    _linkSubscription?.cancel();
    super.dispose();
  }

  Future<void> initDeepLinks() async {
    _appLinks = AppLinks();

    // Handle links
    _linkSubscription = _appLinks.uriLinkStream.listen((uri) {
      openAppLink(uri);
    });
  }

  void openAppLink(Uri uri) {
    //extract the path from the uri
    final path = uri.path;
    //get id from path
    final id = path.split('/').last;
    debugPrint('ID: $id');
    context.pushNamed(
      ClientAppRoute.clientEventDetailPage.name,
      pathParameters: {'id': id},
    );
  }

  @override
  Widget build(BuildContext context) {
    final goRouter = ref.watch(buildRouterProvider);
    return DynamicTheme(
        themeCollection: themeCollection,
        defaultThemeId: AppThemes.light,
        builder: (context, theme) {
          return OverlaySupport.global(
            child: MaterialApp.router(
              routerDelegate: goRouter.routerDelegate,
              routeInformationParser: goRouter.routeInformationParser,
              routeInformationProvider: goRouter.routeInformationProvider,
              debugShowCheckedModeBanner: false,
              theme: theme,
            ),
          );
        });
  }
}```
angwandi commented 5 months ago

I have also tried the code below but with no success :


  void openAppLink(Uri uri) {
    _navigatorKey.currentState?.pushNamed(uri.fragment);
  }```
llfbandit commented 5 months ago

This issue isn't related to this package. Despite this and to give you some hints, you try to navigate but the current context is above your MaterialApp in the widget tree. If you want to use a GlobalKey<NavigatorState>, you must set it to MaterialApp like in the example project.