ebg11
ebg11

Reputation: 1046

Removing listeners before dispose 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.

Upvotes: 31

Views: 10272

Answers (1)

Md Golam Rahman Tushar
Md Golam Rahman Tushar

Reputation: 2385

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.

Upvotes: 45

Related Questions