I want new (fresh) flutter page widget every time I open it - flutter

When I leave a page widget and come back to it later, it retains the previous info on the previous session. I would like to create a fresh page widget from scratch every time I opened it. How do I implement this?

I am guessing that your issue is the after going to new page using Navigator.push(), you get the previous state of the page after popping the new page.
In that case you can call a setState after popping the new page. Do the following :
Navigator.pop() on Page2 :
Navigator.pop(context,1); /// Passing an extra parameter (1) to Page1 after popping
Navigator.push() in Page1 :
Navigator.push(context,MaterialPageRoute(builder: (context) => Page2())).then((value){
if(value == 1)
setState(() {}); /// Here you can keep a conditional refresh like if you pass 1 in Navigator.pop() then only will it refresh the page
/// It's useful when you want to refresh the page only for specific conditions.
});
The .then() function after Navigator.push() will be called after Page2 is popped and as mentioned in that function, a setState() will be called refreshing the page.

Are you sure that when you are navigating through the pages you close them properly (end their life circle). If not try to navigate with Are you sure that when you are navigating through the pages you close them properly (end their life circle). If not try to navigate with pushReplacement instead of push

Putting the main data of the widget in the widget declaration works. I suppose the problem was the scope of the data used. If outside the widget declaration, the data would basically be global and would be the same every time the widget is navigated to.

Related

Flutter BLoC Cubit back button history

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()

Problem with using `Navigator.pop` and setState in same method

I have stateful widget that is basically list of tasks. In that widget i also have a button that causes Dialog box to appear.
Dialog box is AlertDialog that consists of text field that has controller inside of it and Save button. Button calls state's method that does few things:
dialog box to pop so to get back to list of tasks
causes setState of stateful widget with value from controller
controller to clear
Method looks like this:
void saveNewTask() {
Navigator.of(context).pop();
setState(() {
toDoList.add([_controller.text, false]);
});
_controller.clear();
}
This method is passed all the way down to Save button, and Dialog box is of course seperate widget so Navigator gets the right context.
Considering the order in saveNewTask i would assume the order would go like this:
Dialog box gets poped off
setState gets called, stateful widget gets rebuild
controller is cleared.
but in slow motion i can see this operations don't happen in this order. Controller is cleared and widget is rebuilt before navigator pops off dialog box.
Before cliking the Save Button : -
After clicking the Save Button : -
(and after the second picture dialog box gets removed)
Digging through source code i found out that pop causes another setState of navigator's state that removes the Dialog box.
I thought that maybe popis asynchronous considering the behavior but Future is not returned so i can't use asynchronous function or thento enforce the order. setState is of course not asynchronous so i don't have a clue what's happening here??
Any clue is greatly appreciated.
The procedure you're following is not the actual way to do it.
Reasons:
AlertDialogue and the other dialogues you show are not a part of your main screen widget. It's a totally different widget. So you can't actually setState() of your widget from here.
Now you're trying to set the state of the dialogue widget which is already being removed when you say Navigator.pop();. Which is not possible. Cz that widget doesn't exist anymore.
In normal situations that controller would get destroyed too with the alert widget. But it's a good practice to manually destroy it.
So, to do it the right way these are the steps you should follow ->
First return the controller value to the main widget by passing it to the Navigator.pop(); function. In your case, just pass it for the save button value. Like this:
Navigator.pop(context, controller.text);
Now await on the showDialogue() method. And receive the value to the main widget. Something like this.
String? data = await showDialog(
context: context,
builder: (context) => AlertDialog());
Now in the main widget check if the dialogue returns a string or not. If it does, then set the state with the data you just received. And if it doesn't. In your case, when the cancel button is clicked. It will return null. Then don't do anything.
And finally, if you want to clear the controller for some reason. Then clear it before popping. And if you want to dispose it. Just dispose it inside the dispose method of your stateful widget.

how to prevent PageView.builder building page I didnt enter yet or delete getxcontroller after I leave the page - flutter/GetXcontroller

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

How to keep count of how many times a page is loaded

I want to clear the form fields that remain filled if I click the back button to go back to that forms screen. Thus, basically i want to call a function that will ALWAYS be executed once a page is navigated to so that i can call the controller.clear() function. Thanku.
Can you call controller.clear() right before you navigate to a new screen from the forms screen. This way when they navigate back it will already be cleared.
if you want to call a function when a page is loaded for the first time. the initState of the state class is the place you wann do your calls. because the initState is the first method that gets called when a widget gets mounted on the screen, even before the first render.
#override
void initState(){
super.initState();
// call the functions you want to call
}

How to reload a page on Navigation.pop in Flutter?

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.