skip to Main Content

I’m having trouble creating deeplinks with app_links.
Package: https://pub.dev/packages/app_links

Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: _navigatorKey,
      initialRoute: "/",
      onGenerateRoute: (RouteSettings settings) {
        Widget routeWidget = defaultScreen();
        String? routeName = settings.name;
          if (routeName == '/reset-password/') {
            // redirect to Reset Password Page
          } else {
            // redirect to Home Page
          }

What I want is if I click on the link that contains the url "reset-password"
Ex: https://mywebsite.com/setting/reset-password/[email protected]
it will be executed and redirected to the reset-password page. Additionally it redirects to the home page.

2

Answers


  1. import 'package:flutter/material.dart';
    import 'package:app_links/app_links.dart';
    

    Initialize the AppLinks object and set up the deep link handling in your main() function:

    void main() {
     AppLinks appLinks = AppLinks();
      appLinks.checkInitialUri().then((Uri? uri) {
      if (uri != null) {
          handleDeepLink(uri);
       }
     });
    
     runApp(MyApp());
     }
    

    define the handleDeepLink() function and use the Navigator to navigate to the desired screens:

         void handleDeepLink(Uri uri) {
            if (uri.path.contains('/reset-password')) {
             // Extract necessary parameters from the URI, if required
           String? email = uri.queryParameters['email'];
           String? token = uri.pathSegments.last;
    
           // Navigate to the Reset Password Page
           _navigatorKey.currentState?.pushNamed
            ('/reset-password', arguments:       {
             'email': email,
             'token': token,
             });
           }
         }
    
      Widget build(BuildContext context) {
      return MaterialApp(
         navigatorKey: _navigatorKey,
         initialRoute: "/",
          onGenerateRoute: (RouteSettings settings) {
           String? routeName = settings.name;
           if (routeName == '/reset-password') {
            // Retrieve the arguments passed through the route
            Map<String, dynamic>? arguments = 
              settings.arguments as Map<String, dynamic>?;
    
         // Call your Reset Password page widget and pass the arguments
         return MaterialPageRoute(
           builder: (context) => ResetPasswordPage(
             email: arguments?['email'],
             token: arguments?['token'],
              ),
            );
            } else {
            // Return the default route
            return MaterialPageRoute(builder: (context) => HomePage());
          }
        },
        home: defaultScreen(),
        );
     }
    
    Login or Signup to reply.
  2. you can try with this dependacy go_router 9.0.3

     void main() => runApp(MaterialApp.router(routerConfig: router));
    
     //The route configuration.
     final GoRouter _router = GoRouter(
     routes: <RouteBase>[
      GoRoute(
          path: '/',
         builder: (BuildContext context, GoRouterState state) {
            return const HomeScreen();
          },
         routes: <RouteBase>[
            GoRoute(
              path: 'details',
              builder: (BuildContext context, GoRouterState state) {
                return const DetailsScreen();
              },
            ),
          ],
        ),
      ],
    );
    

    In android AndroidManifest.xml

    <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
     <intent-filter android:autoVerify="true">
       <action android:name="android.intent.action.VIEW" />
       <category android:name="android.intent.category.DEFAULT" />
       <category android:name="android.intent.category.BROWSABLE" />
       <data android:scheme="http" android:host="example.com" />
       <data android:scheme="https" />
     </intent-filter>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search