How to update state when Navigating back to a page Flutter - flutter

i am creating a social media like app, when user clicks on post ,it redirects to a commentpage,in commentspage data is coming from a seperate API,so when go back using Navigator.pop(context), the data in home is not updated, if pressed like button,its not reflecting in ui, but updating in Api,
i wrapped Navigator with setstate but its not reflecting,

Use this on navigator push Navigator.push(context).then((value)=>setState((){}); when it pop the screen it will run the setState

Use this sentence to go to the second page so the function will wait second screen to close
bool result = await Navigator.push(context);
if(result){setState((){});}
when u close the second screen use this sentence Navigator.pop(context, true);
so when u comment has been posted successfully, u will back to screen one and complete the function if result boolean is true, else do nothing.

Related

Flutter Navigator not working as expected

So I want a user to be able to go back to the '/' named route after he/she reaches upto a certain page. The condition is that when that page is reached, user should be routed to '/' only on Navigator.pop(context).
ie., Lets say I have routes configured such that :
User is at '/' > Clicks a button and pushNamed to '/abc' > Clicks a button and pushNamed to '/xyz' > Clicks a button and pushNamed to '/mnq'
Now, I want that when user is at /mnq and Navigator.pop(context) is executed, user should be sent back to '/' and not anywhere else.
When I'm at /xyz, I tried to do a Navigator.pushNamedAndRemoveUntil(context, '/mnq', ModalRoute.withName('/'), arguments:....) but It does not work because when Navigator.pop is executed after reaching /mnq, I see a black screen.
Is there a way to do this. Please note that I need to send arguments from /xyz to /mnq.
You can try use pushReplacementNamed()
'/' pushNamed to '/abc' pushReplacementNamed to '/xyz' pushReplacementNamed to '/mnq'
read this article for more information
pushNamedAndRemoveUntil is remove all page before. when you use back/pop, it will show black because no page before. you can pass argument with pushReplacementNamed Flutter - Pass values between routes using pushReplacementNamed or store data in local https://pub.dev/packages/shared_preferences
1st. on pressing a button to user should be sent back to '/'
simply use Navigator.popUntil(context, ModalRoute.withName('/'));
in place of Navigator.pop(context);
2nd. if user tab back button
WillPopScope(
onWillPop: () async {
Navigator.popUntil(context, ModalRoute.withName('/'));
return false;
},
child: Scaffold(......),
)

Anyone Please Help. Listview on flutter not updating

https://i.stack.imgur.com/8yq5w.png
when I click 1st time on Shirshak it doesnot open anything. When I try to click on mobile 2nd time on Shirshak it opens new Screen https://i.stack.imgur.com/4fVMN.png. And when I click 3rd time on mobile in 2 then It opens Shirshak again. But when I click it 4th time then it opens 2 https://i.stack.imgur.com/kqEqv.png . This happens again and again I have to hit same two times to get inside chat screen in flutter.
Here is my code:
https://i.stack.imgur.com/iGYfZ.png
https://i.stack.imgur.com/MdY1G.png
https://i.stack.imgur.com/tIUKh.png
The function getDataForChat() in line 97 is an asynchronous method but you didn't use await and ansync keyword for it ( many other method which collect data from the internet in your code isn't marked with async and await too).
You should disabled all the other button when anyone of them is clicked .
You are using a Stateful Widget, so to change the value of any variable, you must use setState . For example, in line 101, you should use setState to set isLoading to false.

Navigating to the updated page in flutter

When I insert data from other page and pop this page to go back on the listing page, I have to refresh the listing page to get the updated list of data. I want to go back to the updated version of page without pressing refresh button. I am using pushNamed and also I want to have a back arrow button on the input page.
Using Navigator.push or Navigator.pushNamed returns a Future object to you.
This Future resolves when you use Navigator.pop in the your second screen.
Here is a basic example. Assuming you have function goToNewPage which you are calling to push a new Screen, you can do it like this.
class Screen1State extends State<Screen1> {
void goToNewPage (context) {
Navigator.of(context).pushNamed('screen2').then((_) {
// The screen2 has popped. so you can just call setState here to just rebuild
// You can do anything you want here, that needs to happen after the new page has been popped.
setState(() {});
});
}
... rest of your code.
Note, the actual implementation of what you should do inside the callback depends on how your widget is getting data. But you can do anything you want to do after your new page is popped inside that callback.

How to update screen or state on coming back to route using pop

I want to update a screen on coming back to that screen using Navigator.pop.
E.g. I have Home Screen and Some steps screens (Step1, Step2, Step3, Step4)
Home screen is a stateful widget and shows number of steps completed.
The flow is like
Home => Step1 => Step2 => Step3 => Step4
On any step, on pressing back or pressing close button, it should go back to Home screen and home screen should show updated state of steps completed. So i am navigating to next steps in steps screen using Navigator.popAndPushNamed so that Navigator.pop will go to Home screen.
And using Navigator.pushNamed(context, 'Step1').then in Home screen to update it.
But the problem is .then is called just on Step1 itself because it got popped and if user goes like
Home => Step1 => Step2 => Step3 => Home (using pop)
Home scren will only show Step1 is completed because when going from Step1 to Step2, its .then callback was called and when coming to Home from Step3 using Navigator.pop, no code is being executed and i am unable to update the Home Screen.
I know i can use Navigator.pushNamed(context, 'Home') from steps to go to home screen but it doesn't looks ok (it looks like Home screen was pushed on it instead animation should be like it goes back).
You can use a variable in your previous page which will keep a track, if you are coming from the next page and return a boolean true variable, stating that it has returned from the previous activity. You can use that varible in your init method and if the value of that variable is true you can just fire an event which will return appropriate state.
found the solution using RouteObserver and implemented something like example given in https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
The method i was looking for didPopNext of RouteAware.
Check this, It's Working For Me!
Navigator.of(context).push(route).then((val){
//you can update you data here, when coming back
})

Is there any way to pop several pages and reload the destination page?

I'm setting up an app which have to fill a long 4-part form, which might be displayed in 4 sequential screens in a mobile application. Now on the 4th part of the form, there is a submit button. User clicks on Submit and all the previous screens should be popped and user should be taken back to the Item-List screen(1st screen) where the updated data items must be shown. We won’t be pushing anything new here, just going back to a previous route. But the first page should be reloaded. How is this possible?
// Updated Answer
So if this is your first screen where you are pushing and where you want to call a function when poped, this is how you can do it.
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => RegisterScreen()))
.then((success) {
if(success){
// call your function here
}
});