0

Apologies if my question is a bit dumb but I have just started with flutter.

I am working on sharing and deep link flutter. I have created intent and everything is working perfect except one. I am calling "initUniLinks" on my homepage not mainpage. So link and navigation is working perfect but when I open app through link shared and specific page opens but after that when go to homepage it again gets the "initUniLinks" and again redirects to specified page.

I am using Uni-link plugin and app or deep link method from this https://pub.dev/packages/uni_links

I have tried not too much but some basic options like giving null value to initialLink before navigation but as my widget is in initstate it is called again when I come back to homepage.

class HomePage extends StatefulWidget {
  final Widget child;
  HomePage({Key key, this.child}) : super(key: key);
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {

  String initialLink = "";

  void initState() {
    super.initState();
    initUniLinks();
  }

  Future<Null> initUniLinks() async {
    try {
      initialLink = await getInitialLink();

      if (initialLink != null || initialLink != "") {
        initialLink.contains("pgpost")
            ? await Navigator.push(
                context, MaterialPageRoute(builder: (context) => PostPage()))
            : await Navigator.push(context,
                    MaterialPageRoute(builder: (context) => UserPage()));

      }
    } on PlatformException {

    }
  }
}

I just want to know is there any way that I can call 'initUniLinks" just one time not every time by removing from initstate ? Or any other better solution. Scenario here is simple I want my user to navigate to deep link page only one time when he clicks the link but after that my app should be able to navigate normally.

Muldec
  • 4,641
  • 1
  • 25
  • 44
NilxSingh
  • 253
  • 2
  • 5
  • 17

1 Answers1

0

I guess the problem is you calling navigator from initState, the widget tree is not complete when you navigate to another page, so when you come back to home page the initState is called again. You can use this sheet: WidgetsBinding.instance.addPostFrameCallback((_) => initUniLinks()); in your initState, that will call a function when the widget is fully complete.

Please give me the feedback if its work

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => initUniLinks());
  }
Katekko
  • 335
  • 3
  • 10
  • can u give the full code, so I can run from myself. So I can try figure out what to do – Katekko Jul 16 '19 at 21:15
  • There is no need for code.. it is simple uni-link plugin adoption from documentation which allow us to redirect user to specific screen or page in our app when someone clicks those links (shared or any). I have included link in problem description. Still could not find tell me I will give one. Thanks – NilxSingh Jul 20 '19 at 10:12