Navigating to the updated page in flutter - 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.

Related

how to force build when ever a page shows up in flutter

Hello hope you guys are ok.
the problem I'm facing is we have a main_page which leads to a page doing some changes on data which are show on the main page.
after some process if the user touches back button and goes to main_page app loads it from stack and the data are not shown because it does not get rebuilt.
I don't want to control back button because there are other pages which lead to data changing page and I also tried using valuelistenablebuilder but I don't know why it goes wild and gets into a screen refresh loop without even changing the valueListenable I used redux to manage the value.
actual reason I want main page to rebuild is to call a method. why do I not call that method in second page is complicated and because i don't want to.
in conclusion I want main page to rebuild whenever it shows up even when it's read from the stack or even is there a way to tamper with stack of the pages without visual changes to the user.
you need to use Shared preferences plugin
https://pub.dev/packages/shared_preferences
If I understood the question correctly
The flow should be:
Screen A
#override
void initState() {
loadSomeDataFromDB();
super.initState();
}
Screen B
#override
void initState() { *//INIT AS EXAMPLE*
changeSomeDataFromDB();
super.initState();
}
You can try this in Screen A
onPressed: () async {
bool? shouldLoadDataAgain = await
Navigator.of(context).push(PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
const ScreenB(),
));
if(shouldLoadDataAgain!=null&&shouldLoadDataAgain ==true){
loadSomeDataFromDB();
}
},
and this in Screen B when user press back button
onPressed: () async {
Navigator.of(context).pop(true)
},
solution for my problem was that used redux store had an object and updating object property or a list does not count as variable change to redux so the widget wouldn't rebuild.

How to update state when Navigating back to a page 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.

Flutter - How to Update Home Route After Returning from MaterialPageRoute

This is my App's flow:
How do I update the Home Page when I press back and return to it from the MaterialPageRoute? I attempted to use the setState method in the dispose() method of MaterialPageRoute, but it gave me an error.
You can use Navigator.push(context,MaterialPageRoute(builder: (context) => TheSecondPage())).then((value) { setState(() {}); thus when you pop from TheSecondPage() you will immediatelly call the setState refreshing the page.
Actually, the Home Page is automatically updated, however I made a mistake by updating the variable within the dispose() method, which is invoked after the Home Page has been updated.

Can I know if the screen has been refreshed?

I plan to use the navigator.pop feature to move to the screen before.
Can I know if the screen has been moved to navigator.pop?
I don't want to use a method to pass variables to navigator.pop
If you want to refresh the previous page using navigator.pop, you can use this library GetX to move to the other page, and when you execute navigator.pop, you can refresh the page as shown by the following code:
Get.to(SecondPage()).then((value) => onGoBack());
When calling navigator.pop, onGoBack() is executed.
import 'dart:async';
FutureOr onGoBack() {
setState(() {});
}

Apply method immediately after returning to page in flutter

I have two pages: page A has a "search" function, page B has a "select language" function. I navigate from page A to page B to select my language. Then I want to pop back from page B to page A with the new chosen language and perform the search automatically for that language.
I pass my chosen language from Page B to A by using SharedPrefs (which is working fine). So all I need is when I return to Page A, for the search function to get automatically triggered.
Is that possible in Flutter?
I tried using initState but that only works once when the widget is first built. I also tried didUpdateWidget but nothing happened.
Use this when navigating to PageB from PageA
Navigator.push(context, MaterialPageRoute(builder:(_) => PageB())).then((value) {
// you have come back to the pageA, now perform your logic
callThisMethod();
});