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();
}
}
CodePudding user response:
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();
}
}
CodePudding user response:
You are calling on wrong variable:
@override
void dispose() {
for (TextEditingController controller in controllers) {
controller.dispose(); //<-- change this
}
super.dispose();
}