Firebase Dynamic Link doesn't load the right route in Flutter (Android)

0

I have a problem with Dynamic Links (Fireabase) to load the right route when clicking an url.

  1. I setup a link Review /review in Firebase Dynamic Links
  2. I added SHA 1 and SHA 256 codes to the Android App
  3. The app starts well and it also tracks the clicks inside Firebase.

Only problem I have is it doesn't reconnect the user to the right route. It always goes to the initial route instead of the specified one. I also tried to open and close the app completely .

Here is my code of my main.dart

void main() async {
  SharedPreferences.setMockInitialValues({});
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  FlutterError.onError = (FlutterErrorDetails details) {
    if (!kReleaseMode) {
      FlutterError.dumpErrorToConsole(details);
    } else {
      Zone.current.handleUncaughtError(details.exception, details.stack);
    }
  };

  await FlutterCrashlytics().initialize();

  runZoned<Future<Null>>(
    () async {
      runApp(App());
    },
    onError: (error, stackTrace) async {
      if (kReleaseMode) {
        await FlutterCrashlytics()
            .reportCrash(error, stackTrace, forceCrash: false);
      } else {
        print(error.toString());
        print(stackTrace.toString());
      }
    },
    zoneSpecification: ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String message) {
        if (!kReleaseMode) {
          final time = DateTime.now().toString().split(" ").last;
          message = '${time.substring(0, time.length - 3)}: $message';
        }
        FlutterCrashlytics().log(message);
        if (!kReleaseMode) {
          parent.print(zone, message);
        }
      },
    ),
  );
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    this.initDynamicLinks();
  }

  void initDynamicLinks() async {
    final PendingDynamicLinkData data =
        await FirebaseDynamicLinks.instance.getInitialLink();
    final Uri deepLink = data?.link;

    if (deepLink != null) {
      Navigator.pushNamed(context, deepLink.path);
    }

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
      final Uri deepLink = dynamicLink?.link;

      if (deepLink != null) {
        Navigator.pushNamed(context, deepLink.path);
      }
    }, onError: (OnLinkErrorException e) async {
      print('onLinkError');
      print(e.message);
    });
  }

 @override
  Widget build(BuildContext context) {
    final FirestoreService _db = FirestoreService();
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => OnboardinginfosUser(),
        ),
        FutureProvider(
          create: (context) => ProfileService().getUserById(context),
          catchError: (context, error) {
            print(error.toString());
          },
        )
      ],
      child: MaterialApp(
        title: 'XYZ',
        theme: ThemeData(
            brightness: Brightness.light,
            unselectedWidgetColor: Colors.grey,
            canvasColor: Colors.white,
            backgroundColor: Color(0xFFffffff),
            buttonColor: Color(0xFF001C7D),
            primaryColor: Color(0xFF001C7D),
            accentColor: Color(0xFFFFA800),
            textTheme: TextTheme(
              body1: TextStyle(
                color: Color(0xFF333333),
                fontSize: 16,
                letterSpacing: -0.0003,
                fontFamily: 'Roboto',
              ),

            )), 
       home: SplashScreen(),
        routes: {
          HomeScreen.routeName: (ctx) => HomeScreen(),
          SplashScreen.routeName: (ctx) => SplashScreen(),
          '/review': (BuildContext context) => ReviewFormScreen(),
        },
        onUnknownRoute: (settings) {
          return MaterialPageRoute(builder: (ctx) => HomeScreen());
        },
      ),
    );
  }
}

android
firebase
flutter
dynamic-linking
firebase-dynamic-links

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0