Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing listerers before dispose flutter

Tags:

flutter

If i initialise a watcher in the initState() e.g.

textController.addListener(textTypedListener);

Do i need to manually remove the listener before I dispose of the text controller? or does the dispose automatically handle this.

eg. Options 1

 @override
  void dispose() {
    textController.removeListener(textTypedListener);
    textController.dispose();
    super.dispose();
  }

Option 2

 @override
  void dispose() {
    textController.dispose();
    super.dispose();
  }

Which is best?

Thanks a lot.

like image 655
ebg11 Avatar asked May 12 '20 19:05

ebg11


1 Answers

According to the Interactive Example given in the flutter documentation of Handle changes to a text field, it's commented that calling dispose also removes the listener.

So the second option would be best.

like image 150
Md Golam Rahman Tushar Avatar answered Nov 07 '22 17:11

Md Golam Rahman Tushar