Reputation: 1046
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
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