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

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

Flutter Change Notifier dispose individual listener

I'm currently adding a listener to a screen like this:
#override
void initState() {
person.addListener(() {
doSomething();
});
super.initState();
}
This works, but I have difficulties with correctly disposing the listener after I will call Navigator pop.
I tried calling dispose
#override
void dispose() {
person.dispose();
super.dispose();
}
But this disposes all the listeners on "person". Which are active on other parts in the app.
Is there a possibility to assign a key to the addListener and dispose only that addListener?
Thanks.
You could say person.removeListener(yourListener) in your dispose method

I need removelistener before ChangeNotifier disposed?

For example, i am call the controller.dispose() when page dispose, i also have to call the controller.removeListener?
TextEditingController _controller = TextEditingController();
#override
void initState() {
super.initState();
_controller.addListener(_listener);
}
void _listener() {
print(_controller.text);
}
#override
void dispose() {
// _controller.removeListener(_listener); //It is a must?
_controller.dispose();
super.dispose();
}
I see ChangeNotify source code about dispose
#mustCallSuper
void dispose() {
assert(_debugAssertNotDisposed());
_listeners = null;
}
I think is cleared listener, i am not need to call the removeListener method. But somebody tell me i need call the reamoveListener method before dispose method. I feel confused and want someone to tell me i am right or wrong. Thanks in advance!
Just called
_controller.dispose();
you don't need to call _controller.removeListener(_listener);

Is it possible to ensure that a controller is disposed in Flutter?

I have several controllers in my code (eg. TextEditingControllers, StreamControllers, ScrollControllers). Is there a way to get a warning/error if I ever forget to call the dispose() method on this variables?
I was hoping that there could be a lint for pedantic, but I couldn't find anything.
To clarify:
If initialize a controller
TextEditingController searchFieldController = TextEditingController();
And then forget to call
#override
void dispose() {
searchFieldController.dispose(); // This line
super.dispose();
}
It should generate a warning

Should codes be written before super.initState(); or after in Flutter?

Should the code that is being written to initState() function be written before super.initState(); or after?
Which one is proper:
#override
// code here
super.initState();
}
or
#override
super.initState();
// code here
}
both will work.
But if you see from any dependencies or official docs flutter, write your code in initSate() after super.initState();
#overrride
initState(){
super.initState()
//your code
}
reference to this initState
the opposite for dispose(), write your code before super.dispose();
#overrride
dispose(){
//your code
super.dispose()
}
reference to dispose
When I see #Kahoo answer, I check it by cmd + click at super.dispose and super.initstate, I found this for dispose
/// If you override this, make sure to end your method with a call to
/// super.dispose().
///
/// See also:
///
/// * [deactivate], which is called prior to [dispose].
#protected
#mustCallSuper
void dispose() {
assert(_debugLifecycleState == _StateLifecycle.ready);
assert(() {
_debugLifecycleState = _StateLifecycle.defunct;
return true;
}());
}
abstract class State :
/// If you override this, make sure your method starts with a call to
/// super.initState().
#protected
#mustCallSuper
void initState() {
assert(_debugLifecycleState == _StateLifecycle.created);
}
Both will work fine, but the better practise is to write before super.initState(), because it will do all the initialisations before creating the state widget which will help you in maintaining the check on the Widget State.
But this does not means that the second method will not keep close eye on maintaining the state, But as of better practise the First way is preferred.

`RouteAware`-implementer unsubscribe from any previous dependency?

The following snippet is from the Flutter docs of RouteObserver:
#override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context));
}
#override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
}
I don't understand why didChangeDependencies does not include routeObserver.unsubscribe(this) as well.
Shouldn't the RouteAware-implementer unsubscribe from any previous dependency?
That is safe because the implementation of subscribe does nothing if this is already on the subscribers list.