Ad

Firebase Dynamic Link With Dynamic Parameters?

I'm trying to add a dynamic parameter to my dynamic link. For example, if I have a link as https://xyz.page.link/promo?id=1 added to the firebase console. I want that id=1 should be dynamic i.e using the same link I could pass on this for different ids rather than creating links for each id and checking each time into the code to redirect to the Flutter screen from getRoute() mentioned below.

Can anyone suggest some way to achieve this?

// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';

// Project imports:
import 'package:xyz/src/ui/home_screen.dart';
import 'package:xyz/src/ui/promo_screen.dart';

class DynamicLinksService {

  static Future<Widget> initDynamicLinks() async {
    final PendingDynamicLinkData? data = await FirebaseDynamicLinks.instance.getInitialLink();

    if (data != null){
      return getRoute(data.link);
    }
    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData? dynamicLink) async {
          return getRoute(dynamicLink!.link);
    }, onError: (OnLinkErrorException e) async {
      print('onLinkError');
      return e.message;
    });
    return HomeScreen();
  }
  static Widget getRoute(deepLink){
    if (deepLink.toString().isEmpty) {
      return HomeScreen();
    }
    if (deepLink.path == "/promo") {
      final id = deepLink.queryParameters["id"];
      if (id!= null) {
        return PromoScreen(id: id);
      }
    }
    return HomeScreen();
  }
}

Thanks

Ajinkya Taranekar

Ad

Answer

I can share with you the code I use to create dynamic links with a parameter:



class DynamicLinksService {
  static Future<Uri> getGroupDynamicLink(
      BuildContext context, String id) async {
    final DynamicLinkParameters parameters = DynamicLinkParameters(
      uriPrefix: 'https://my_page.page.link',
      link: Uri.parse(
          'https://my_page.page.link/add_group/?id=${id}'),
      dynamicLinkParametersOptions: DynamicLinkParametersOptions(
          shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
      androidParameters: AndroidParameters(
        packageName: 'com.my_page',
        minimumVersion: 0,
      ),
      iosParameters: IosParameters(
        bundleId: 'com.my_page',
        minimumVersion: '0',
        appStoreId: '122343564',
      ),
      socialMetaTagParameters: SocialMetaTagParameters(
          title: AppLocalization.of(context).getMessage('join_info_group'),
          description: group.title,
          imageUrl: Uri.parse(group.image)),
    );

    final link = await parameters.buildUrl();
    final ShortDynamicLink shortenedLink =
        await DynamicLinkParameters.shortenUrl(
      link,
      DynamicLinkParametersOptions(
          shortDynamicLinkPathLength: ShortDynamicLinkPathLength.unguessable),
    );

    final Uri dynamicUrl = shortenedLink.shortUrl;

    return dynamicUrl;
  }
}

Just change the values to your project values and data.

Ad
source: stackoverflow.com
Ad