handle link and navigate with firebase dynamic links in flutter

Viewed 1312

I am using firebase_dynamic_links 0.5.0 in my flutter project app. I am using bloc for state management and I have the issue with the navigation. I am handling the link normally but when I try to navigate to another page that's not work . I don't know why ?

PS : I have test the firebase_dynamic_links example and that's work normally


main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Production code
  ErrorWidget.builder = (FlutterErrorDetails details) => Container();


  runApp(MyApp());
}

// Myapp class here I get the result of print but navigation don't work

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _authBloc = AuthBloc();

  final _progressBloc = new ProgressBloc();

  @override
  void initState() {
    super.initState();
    initDynamicLinks();
  }



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

    //print("hhhhhhh" + deepLink.path);

    if (deepLink != null) {
      print("Link :" + deepLink.path);

      // this line don't work 
      Navigator.pushNamed(context, '/helloworld');
    }

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

      if (deepLink != null) {
        print("Link :" + deepLink.path);

        Navigator.pushNamed(context, '/helloworld');
      }
    }, onError: (OnLinkErrorException e) async {
      print('onLinkError');
      print(e.message);
    });
  }

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = ThemeData(
      fontFamily: Constants.DEFAULT_FONT_NAME,
      textTheme: Theme.of(context)
          .textTheme
          .apply(fontFamily: Constants.DEFAULT_FONT_NAME),
      brightness: Brightness.light,
      canvasColor: Colors.black12,
      scaffoldBackgroundColor: Colors.white,
      primaryIconTheme:
          Theme.of(context).primaryIconTheme.copyWith(color: Colors.white),
      accentIconTheme:
          Theme.of(context).accentIconTheme.copyWith(color: Colors.white),
      primaryTextTheme:
          Theme.of(context).primaryTextTheme.apply(bodyColor: Colors.white),
      primaryColorLight: Colors.white,
      primaryColorBrightness: Brightness.light,
      primarySwatch: Constants.COLOR_PRIMARY,
      primaryColor: Constants.COLOR_PRIMARY,
    );

    var home = Stack(children: <Widget>[
      RootPage(),
      ProgressDialog(),
    ]);

    return ProgressProvider(
      dialog: _progressBloc,
      child: AuthBlocProvider(
        bloc: _authBloc,
        child: StoreBlocProvider(
          store: Store(),
          child: RootProvider(
            bloc: new RootBloc(),
            child: MaterialApp(
              localizationsDelegates: [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('en'), // English
                const Locale('fr'), // Hebrew
              ],
              title: 'Foodynasty',
              theme: themeData,
              debugShowCheckedModeBanner: false,
              routes: <String, WidgetBuilder>{
                '/': (BuildContext context) => home,
                '/helloworld': (BuildContext context) => RestaurantShare(
                      restaurantId: 'test',
                    ),
              },
            ),
          ),
        ),
      ),
    );
  }
}

thank you, and sorry for my bad English.

1 Answers

It could be that the deepLink is null and the code inside the if statement is not executed. Could you provide an example of the deep link you are sending to your app? It could be that firebase_dynamic_links cannot parse the deep link correctly if it has an error.

Related