Removing listerers before dispose flutter

Viewed 4045

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.

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.

Related