what we can dispose in flutter? is only controller or else? - flutter

what we can more do to avoid memory leak exception
#override
void dispose() {
customerNameController.dispose();
timer.cancel();
super.dispose();
}

Related

flutter memory leak my widget in homescreen

this is the error cause when I return to home screen. Is there any way to solve this? I tried dispose my timer but it did not work
bool containerClicked = false;
int nextSpinValue = 0;
int? widgetIndex = 0;
var spinController = StreamController<int>.broadcast();
void spin() => spinController.add(++nextSpinValue)
#override
void initState() {
Redirects.drawerList();
super.initState();
}
#override
void dispose() {
_timer!.cancel();
super.dispose();
}
#override
Widget build(BuildContext context) {
var mHeight = MediaQuery.of(context).size.height;
var mWidth = MediaQuery.of(context).size.width;
final _formKey = GlobalKey<FormState>();
var spinController = StreamController<int>.broadcast();
int nextSpinValue = 0;
void spin() => spinController.add(++nextSpinValue);
Timer.periodic(const Duration(seconds: 3), (timer) async {
if (nextSpinValue >= 3) {
nextSpinValue = 0;
}
spin();
});
caused somewhere in setstate
The problem happens due to the fact that StreamController has to be closed. Otherwise, StreamController object stays in the memory even if the widget getting stream events no longer exists. dispose() is a good place to call the close method.
AFAIK it's a property of Flutter Controllers. You have to close them.

Flutter - Do controllers need to be disposed before they're reassigned?

Say I have a late TextEditingController. It's initially constructed somewhere
#override
void initState() {
super.initState();
controller = TextEditingController();
}
....
// then somewhere else I reconstruct it
// controller.dispose() // is this needed before the reassignment?
controller = TextEditingController();
Thanks!
No Before initialise no need to dispose. But Yes, its good practice that when TextEditingControlleris no longer needed you need to dispose it.So you can dispose your all TextEditingControllers in dispose method.
void dispose() {
controller.dispose(); //textFiledName
super.dispose();
}
So dispose method triggered whenever the created object from the stateful widget is removed permanently from the widget tree, so Dispose releases allocated memory.
More details checkout this official link
TextField
dispose

How to dispose a list of text editing controllers in Flutter

I want to know what is the right way to dispose all text editing controllers in a list in flutter?
List<TextEditingController> controllers = [];
I tried this, but it's not working. What should be the correct approach for it?
dispose(){
for(TextEditingController controller in controllers){
controller.dispose();
}
}
You are trying to call dispose on your List<TextEditingController>and not on your TextEditingController. You need to change it inside your for loop body.
dispose() {
for(final TextEditingController controller in controllers){
controller.dispose();
}
}
You are calling on wrong variable:
#override
void dispose() {
for (TextEditingController controller in controllers) {
controller.dispose(); //<-- change this
}
super.dispose();
}

is there a way to run timer in background?

I am able to run timer when the screen is loaded but I want to run that same timer in background also is there a way to do that? and I also want to convert that 30 to 30 minutes need help.
static const maxMinutes = 30;
var minute = maxMinutes;
Timer timer;
void initState() {
// TODO: implement initState
super.initState();
startTimer();
}
void dispose() {
// TODO: implement dispose
super.dispose();
minute;
startTimer();
}
void startTimer(){
timer = Timer.periodic(Duration(seconds: 1), (timer) {
if(mounted) {
setState(() {
if (minute > 0) {
minute--;
} else {
timer.cancel();
}
});
}
});
}
timer is running but i want to run the timer in background

How to detect when flutter app comes back from background?

I would like to detect when flutter app comes back from background.
In other SDKs for crossApp development, there is usually a listener when app changes this state.
Is there anything similar in flutter?
class _AppState extends State<App> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
//add an observer to monitor the widget lyfecycle changes
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
//don't forget to dispose of it when not needed anymore
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
late AppLifecycleState _lastState;
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed && _lastState == AppLifecycleState.paused) {
//now you know that your app went to the background and is back to the foreground
}
_lastState = state; //register the last state. When you get "paused" it means the app went to the background.
}
}