Reputation: 549
here when iam creating login page for my application i was stuck here that when i press the login button with valid credentials i am navigating to homescreen but when i double tap on the login button its navigating to homescreen but creating two screens of homescreens(homeScreen is opening two times when press ack button in homepage its again showing homescreen how many times i press loinbutton it creating those many screnns and i have to press backbutton that many times to go back to login from homescreen
Upvotes: 2
Views: 1849
Reputation: 792
The Actual way to avoid Multiple navigations in a flutter.
Just use the below code:
WidgetsBinding.instance.addPostFrameCallback((_) {
//Your Navigation code will be here
});
What it Does?
According to Flutter documentation, this code Schedule a callback for the end of this frame. The provided callback is run immediately after a frame, just after the persistent frame callbacks (which is when the main rendering pipeline has been flushed).
This method does not request a new frame. If a frame is already in progress and the execution of post-frame callbacks has not yet begun, then the registered callback is executed at the end of the current frame. Otherwise, the registered callback is executed after the next frame (whenever that may be, if ever).
The callbacks are executed in the order in which they have been added. Post-frame callbacks cannot be unregistered. They are called exactly once.
See also:
scheduleFrameCallback
, which registers a callback for the start of the next frame.
Upvotes: 0
Reputation: 267454
If I got you right then you can try this logic.
bool flag = true; // member variable
// this goes in your onPressed() method of the button
if (flag) {
flag = false;
// enable click to take user to home screen
}
Upvotes: 1