skip to Main Content

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();
  }
}

2

Answers


  1. 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();
      }
    }
    
    Login or Signup to reply.
  2. You are calling on wrong variable:

    @override
    void dispose() {
      for (TextEditingController controller in controllers) {
          controller.dispose(); //<-- change this
      }
      super.dispose();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search