Home > database >  Flutter disposing large no of TextEdititingController
Flutter disposing large no of TextEdititingController

Time:12-13

I have no of TextEditingController a, TextEditingController b.......z

1) How to short cut way dispose all TextEditingController

@override
  void dispose() {
    // _nameCtrl.dispose();
    a.dispose();
    b.dispose();
     .....
     ....
    z.dispose();
    super.dispose();
  }
  1. if don't dispose the TextEditingControll var , will it caused problem ?

thanks Wing

try to simplify and shorten codee

CodePudding user response:

as per my knowledge:

dispose means that you are Clean up the controller when the widget is removed from the widget tree. So when tree are rebuild, eg: you are calling setState the widget tree will rebuild, and because you already dispose all the controller, they will not consume any memory since they are not exist in the widget tree.

CodePudding user response:

Well according to a great article on the internet :

If you create objects and do not free the memory used by them before their destruction, there will be chances your application will through a memory leakage in the app store or the play store. It is the exit point of the Stateful Widget. Execution of dispose method is done in the end when the state object had built itself enough times and now there is no need for it to build again.

Which means that it is a good practice to always dispose off all your controllers as they can cause an issue later on.

And as for your shortcut then why don't you use a for loop iterating over a list containing all the controller names that you've used in your Stateful widget:

List tec = [a,b,c,d...z];
for(var element in tec) {
  element.dispose();
}
  • Related