The questions is What happened when I call the .dispose() feature with any controller(For instance TextEditingController).
there is a example code at the below:
class SearchPage extends StatefulWidget {
const SearchPage({Key? key}) : super(key: key);
@override
State<SearchPage> createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchController = TextEditingController();
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_searchController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Search Page"),
),
);
}
}
I have searched the internet but I couldn't find satisfaction answer.So,What happen, when I use the _searchController.dispose()?
CodePudding user response:
As far as I remember this method is being called before it gets removed from the widget tree. Like a cleanup method. So after you disposed something you can't access it anymore and it will throw a error if you still try to access it.
CodePudding user response:
- It checks if the object is still in use or not.
- check if listeners are null.
@mustCallSuper
void dispose() {
assert(_debugAssertNotDisposed());
assert(() {
_debugDisposed = true;
return true;
}());
_listeners = _emptyListeners;
_count = 0;
}
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed)
Also try using super.dispos() on end part.
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
State dispose
Called when this object is removed from the tree permanently.
Once you dispose of something, It is removed from the tree permanently.
You can check this question