I have 3 pages - HomePage, FirstPage, SecondPage. HomePage contains a futureBuilder which will fetch data from Server and display it in the HomePage. From HomePage I can navigate to FirstPage (using Navigator.push()) and from there to SecondPage. In the SecondPage I am sending some new data to server. A progress indicator will be shown while sending the data. Now if I press back button two times I will reach the HomePage back. But now the data in HomePage is the same as before. i.e, The data is not updated in the HomePage even if the data is updated in the server. I have button in HomePage which onTap will call setState() and then the Data will be Updated.
But I want the data of HomePage to be updated just after the data is sent from the second page. i.e, from second page we will first show a progress indicator, then we will send data to server, then we will update the data of home page in some way, then only we will stop the progress indicator.So if then we tap back button and return to HomePage, Home page should show the updated data. I don't want to call setState() after clicking back button to home page because it will cause to show a progress indicator(a delay in fetching updated data) again.
Is there any method to do this? Or is there any way to call the setState() method of HomePage from the secondPage ?
Use flutter provider package. Fetch data from Server using global variable. After send the data to server use ChangeNotifier()
function. (Notes: learn about provider package)
Related
I have a project that I had initially setup with Navigator and have since converted over to use BLoC Cubit.
When I use Navigator.of(context).pop(); to go back from a screen that was navigated to using context.read<AppCubits>().pageOneScreen(); I get a black screen instead of being directed back.
Does BloC Cubit's not allow or keep any history like Navigator does?
**** MORE DETAILS ****
I have a back button that would simply call
Navigator.of(context).pop()
and bring the user back to their previous page.
Now with Cubit, using the above doesn't work and returns a blank screen as there is no context stored.
So I have been searching for a solution to this but every one of them seems very verbose, is this type of feature not a part of Cubit or BLoC?
To navigate from page to page currently I am using
context.read<AppCubits>().pageOneScreen();
context.read<AppCubits>().pageTwoScreen();
This works as expected but I am looking for a solution to adding a call that will navigate back to the previous screen/state that was loaded. So if I was on pageTwoScreen, I could click back if I had come from pageOneScreen and have the state changed or at least be directed back to pageOneScreen.
Do I have to create a list and store the state value as a form of rudimentary history and then pop off the previous value and call something akin to
context.read<AppCubits>().historylistvalue()
sorry in advance if i didnt describe my question clearly:
I have several pages made by PageView.builder, while swiping these pages on main screen left and right, I found their initState is already working, and GetXController inside are also loading their data. I use Get.delete<Controller> while clicking back button and coming back to main screen to remove these data.
However, the state and data will bring back to main screen if I didnt activate Get.delete<Controller>, and therefore next time I come to this page, the state of page will be not default, for instance the button still keep pressed status.
So my question is how to prevent page.builder to build the widget before I come inside to this page, or how to delete getxcontroller if I am not in this page. Thanks a lot!
Try resetting the GetX by calling
Get.reset(); --> this will re-initialize controllers
Alternatively calling
Get.put(ControllerName()); in a build method will re-initialize the controller
When the user logs in, they get pushed to the homepage and their data is retrieved from database on initstate and then the widgets are built. However, i noticed for the widgets that are built without a streambuilder, where i manually retrieve data once, are duplicated if i navigate to another page and then come back. When I say duplicated I mean the exact same data is redundantly copied again using the same widget, so like a users' profile pic could show up twice. I am using a listview most of the time for these cases/
How do i avoid this and just built the widgets once so if the user goes back the page stays the same?
I want to have page A reload, re-run the Http requests, once I pop back to it from another page that lets you edit the data page A displays. The other page is triggered by a long press in a ListView generated by a function within page A. IE:
Page A:
_PageAState
Reload function I need to call
Scaffold
List item
Text
ETC...
Expanded
ListView
Gesture Detector that navigates
ETC
How can I trigger a function of any sort or make the page reload when I pop back to it?
I've tried passing data from the pop and using an if statement but the nested structure messes it up.
of course, there's a way to do that!
As you realized, the first route needs to get notified when the pushed Route gets popped.
Thankfully, there's a tutorial in the Flutter documentation that handles returning data from Routes.
In your case, no data gets returned but you can still use the same principle to wait for the route to be popped: Simply await the Navigator.push call!
This will return once the other route gets popped.
await Navigator.push(
context,
MaterialPageRoute(builder: (context) => OtherPage()),
);
// TODO: Now, refetch the data.
I have a home icon in the navbar whenever the user will click on it, It will take the user to the Home Page. In the Home Page, there are few form fields that I don't want to change when coming back to the Home Page from the navbar Home icon. The following is the code that I am using:
back_to_home(){
this.navCtrl.push( HomePage )
}
By using the above, state is refreshed and changes. Is there way to change the state without refreshing the state?