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

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
}

Related

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

Why "this" changes also without a new class statefull widget screen instance?

I have a screen as statefull widget. When I instance it I have the same "this" in initState and build methods, as expected.
But if, from that screen, I open a new screen and than I come back to the first screen, first the statefull widget screen doesn't appear to be reinstanced, the method initState is not called again but the method build is called (as expected, the screen has to be drawed again) and it has a new "this" than the first time the screen has been displayed.
I could also accept the screen class has a new this but how is it possibile without a new instance of the screen widget?
It seems the class change its "this" without a new instance of the class.
Is it possibile? Why? Am I wrong?
By documents of flutter's stateful initstate
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Which means when you added the stateful widget for the first time this method is called and this (instance) is created..
Now the build method is called on various situations
The framework calls this method in a number of different situations. For example:
After calling initState.
After calling didUpdateWidget.
After receiving a call to setState.
After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
After calling deactivate and then reinserting the State object into the tree at another location.
So each timethe build is called a new instance is created if you have added the code to create an instance in build method.
When you move to the next page the first screen is still in widget tree. Then you pop screen 2 so you see screen1 ahain but this time its not added to the widget tree so only the build is called.. if you do navigator.push from screen 2 and navigate to screen 1 you will see initstate being called again because a new instance of screen1 is added to the widget tree..

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

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.

Why does the initState method of each page in TabBarView gets called every time a tab is changed

I would like to do some startup work on a page and I decided to put it in the initState method of a page however I noticed that every time I switch a tab the initState method of various other pages included in the TabBarView get called even when those pages are not requested. Is this normal behavior ? Currently the way I am handling to determine if the page is actually requested to be displayed is by reading the index value of TabController then performing the necessary initState work if needed. My question is am I handling this mechanism properly and also does the initState of each page get in the TabBarView get called whenever the tab changes ?
Not so sure but I think what Flutter attempts to do is to ready nearby tab views (one before and after for example) so that when the user switches views it'll already be loaded and to minimize load times.