Home > Software engineering >  Flutter- Is recommended to dispose() textControllers?
Flutter- Is recommended to dispose() textControllers?

Time:11-17

The thing is have a lot of these TextEditingControllers in many screens, Login, Forms, etc. Do I have to implement a dispose method to each of these controllers do to memory leak or it doesn't matter?

Thank you!!

I tried in some screens but I don't know if is recommended.

CodePudding user response:

In the official Flutter documentation for the TextEditingController class, we find the following:

Remember to dispose of the TextEditingController when it is no longer needed. This will ensure we discard any resources used by the object.

To learn more always refer to the official Flutter documentation first.

CodePudding user response:

you should always dispose your controllers after you're done working with them, the TextEditingController can have a listener after you initialize it, so if you don't dispose it, it will still be running even if you navigate to other screens in your app, and this is valid for all other controllers like AnimationController, StreamController...

the change in the performance actually can't be visually clear for just some, but as your app goes bigger, it will have some performance issues.

  • Related