How to reset variable to default values in getx? - flutter

I am creating an app that has multiple questions (like a quiz) and I am using Getx for state management.
in the main method I initialize controllers as follows
void main() {
Get.put(HomeScreenController());
Get.put(QuestionsController());
runApp(MyApp());
}
in the home screen I have a pageview and I am using HomeScreenController() to manage it.
The last page of the page view has a button to go back to the initial page. For my case, I want to reset all variables in both controllers to their initial values.
I have tried one solution, it worked but I feel like it is tedious to do for other controllers that has so many variables which is calling a method that reset the variables manually.
The other solution that I have tried is to dispose the controllers when the user clicks on the button calling Get.put(HomeScreenController()); in the build method of the home screen.
onPressed: () {
qCon.dispose();
controller.dispose();
Navigator.pushReplacementNamed(context, HomeWrapper.id);
},
but I got an error that says
A HomeScreenController was used after being disposed.
'Once you have called dispose() on a HomeScreenController, it can no longer be used.
is there anyway to do what I am trying to do other than updating variable manually?

Update: Turns out that
Get.reset();
do the job when initializing controllers in the home screen
also calling
Get.put(ControllerName());
in a build method will re-initialize the controller

Related

How not to dispose Stateful widget in Flutter?

I have an app. Its homepage has a lot of functionality(heaver). It takes a little bit time to build. Therefore when App starts I show the Splash screen in the meantime Homepage is built. But when I navigate(by push replacement) from the homepage to another it's dispose method called. So when I try to come back to the homepage it takes some time and the app got hang for that time. So I want that When I navigate(by push replacement) from Homepage to another page its dispose method should not be called. So, when user want to come back to the homepage it doesn't rebuild.
Note: I can not use Navigator.push or something else except push replacement because I am navigating from navigation bar( I made my custom Navigation Bar Where I cannot use anything else except push replacement).

Is the back button the same as dispose() method?

flutter
Is the back button the same as dispose() method ?
I know there's not much similarity, but I mean, when you hit the back button, does that free up memory from the tree of the page we left?
Considering that you're talking about the Framework's BackButton widget, then eventually, why?
Because the framework's back button is just an abstraction of creating your own widget with an IconButton that calls Navigator.maybePop(context); when it is pressed.
To answer your question: yes, if the page is closed when BackButton is pressed, then yes it will release resources (it will call dispose() if your widget is a stateful widget).
Keep in mind that your still need to override the dispose() method and release any resource (if any) so that those resources are release when the back button is pressed.
To sum up::: BackButton simply calls Navigator.maybePop(context); and it will only release resources of your StatefulWidget IF you overrode it AND call some_class.dispose().
No the back button doesn't replace the dispose function if you have controllers in a statefulwidget screen when state object is removed you will need to dispose it to avoid memory leaks.
flutterdartmethodscallbackdispose

RouteObserver use and Widget disposing when navigatin to another route with Flutter

I'm currently developing an app which uses ads.
I've created an Adbanner for testing in the main view.
In the main view i have a lot of buttons that send the user to other routes. Most of this buttons are in another classes and used elsewhere inside the app. I know that to dismiss or dispose the banner i just need to call Dispose() or Dismiss().
My problem is that i'm lookin for a way to dismiss or dispose the bannerAd on the Navigator.push()
I found this and this that doesnt answer my question.
Thanks in advance to anyone who can help

Reload application in SAPUI5

Is there any way by which we can reload the whole SAPUI5 application?
When a SAPUI5 app is called, onInit() function is called and we are initializing some settings for view here. Say if some change (like selection of a checkbox in master view) requires the whole application to be reset, can we reload the application and call onInit() function again to correct our initial settings? Is there any trigger mechanism by which we can call onInit() function explicitly?
Thank you in advance for your suggestions.
Regards,
Raja
If you have the need to reload or reset your application, then I would think your app suffers from a major design flaw... This is definitely something I would look into first if I were you!
However, to answer your question, why not move the initialization code into its own function, and call that function from the onInit() event handler and whenever you need to reset your app?
I suggest you to use some kind of intercommunication (e.g.: EventBus) between your Controller instances, from the master view notify your Controllers to rerun initialization logic like follows:
move you initialization logic to separate functions on each Controller and call this method upon onInit
in onInit subscribe to reinitialize event
in you master view add a listener function to the mentioned checkbox selection event, and publish the reinitialize event to notify your Controllers to reinitialize
Controller.js:
function onInit() {
this._initialize();
EventBus.subscribe('reinitialize', this._initialize);
}
funcion _initialize() {
// init logic
}
MasterController.js:
function onSelectionChange() {
EventBus.publish('reinitialize');
}
Sometimes you cannot directly use the window global variable in sapui5.
Refer to this page:
https://help.sap.com/viewer/825270ffffe74d9f988a0f0066ad59f0/CF/en-US/7d0dba71a2cb45ebb1959daba904ca99.html?q=location%20reload
var myLocation = location;
myLocation.reload();

Android's viewDidLoad and viewDidAppear equivalent

Does Android have an equivalent to Cocoa's viewDidLoad and viewDidAppear functions?
If not, then how would I go about performing an action when a View appears? My app is a tabbed application, in which one of the tabs is a list of forum topics. I would like the topic list to be refreshed every time the view appears. Is such a thing possible in Android?
The Activity class has onCreate and onResume methods that are pretty analagous to viewDidLoad and viewDidAppear.
Activity.onResume
EDIT
To add to this, since some have mentioned in the comments that the view tree is not yet fully available during these callbacks, there is the ViewTreeObserver that you can listen to if you need first access to the view hierarchy. Here is a sample of how you can use the ViewTreeObserver to achieve this.
View someView = findViewById(R.id.someView);
final ViewTreeObserver obs = someView.getViewTreeObserver();
obs.addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
obs.removeOnPreDrawListener(this);
doMyCustomLogic();
return true;
}
});
onResume() is more like viewCouldAppear. :) public void onWindowFocusChanged(boolean) is the closest to viewDidAppear. At this point within the activity lifecycle you may ask the view about its size.
From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:
onCreate(Bundle) is where you
initialize your activity. Most
importantly, here you will usually
call setContentView(int) with a layout
resource defining your UI, and using
findViewById(int) to retrieve the
widgets in that UI that you need to
interact with programmatically.
The equivalent for viewDidAppear is closer to the onResume method:
Called after
onRestoreInstanceState(Bundle),
onRestart(), or onPause(), for your
activity to start interacting with the
user. This is a good place to begin
animations, open exclusive-access
devices (such as the camera), etc.