Search code examples
flutterfirebasepushuilocalnotificationnavigator

Flutter navigator.push, set it to only push once, disallow user to press multiple times to push multiple pages


My app receives in-app notifications from Firebase. When the notification is pressed, it will prompt the user to the page.

My issue is if the notification is pressed multiple times, it will push the same page multiple times, causing to have lots of the pages in the stack. Is it possible to configure and limit Navigator.push to push only once?

I have tried using pushReplacemement but I'm still facing the same issue.


Solution

  • PushReplacement will not work since it always create new instances of widget every time.

    I can see 2 ways to fix it:

    1. Manage while pushing: You can try to store a variable (or as persistent data) that stores if the page has been pushed or not and only push if it is the first time.

    2. Manage while poping: If the number of pages doesn't matter and all you care about is going back to the main screen on pressing back, use the popUntil method to pop until to return to the desired page.

    Although I do not know the context of your application, getting same notification multiple time is probably not a good idea. You should probably change the functionality there instead. (Just a suggestion and do your own research).