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.
Related
Im using getx and i want to call api after bage build, because this api its not necessary part of the page build.. What i know is this way
#override
void initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_){
});
}
But what about getx best way ?
the Getx package offers lifecycle methods that are very convenient in you're cases, what you need here is to use the onReady(), which is called one frame after the onInit() is executed, you can say that its equivalent to the:
#override
void initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_){
// Here is the code which will be executed exactly after initState using StatefulWidget
});
}
you can use onReady() like this:
class TextController extends GetxController {
#override
void onInit() {
/*...*/
}
void onReady() {
// Here is the code which will be executed exactly after onInit using Getx
}
}
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
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);
i'm building a simple app that prints the result of the current TextFormField. Such as when the text changes it prints the new value.
I found out that you can achieve this with TextEditingController.addListener that listens for changes and executes a function.
So i wrapped it all in initState as follows
#override
void initState() {
addressController.addListener(() {
print(addressController.text);
});
The problem I have is that sometimes it records changes even when there aren't any:
This is what happens writing a word and then deleting it.
If you add listener then you should remove it somewhere, otherwise there can be situation when TextEditingController will have 2 or more listeners:
#override
void initState() {
addressController.addListener(_addressControllerListener);
super.initState()
}
void _addressControllerListener() {
print(addressController.text);
}
#override
void dispose() {
addressController.removeListener(_addressControllerListener);
super.dispose()
}
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.