Reputation: 23
I got a problem removing a listener from a ListChangeNotifier from collection_providers. Got the following code:
file one:
class ScriptOne {
static ListChangeNotifier<CustomClass> notifier = ListChangeNotifier<CustomClass>(<CustomClass>[]);
}
file two:
class ScriptTwoWidget extends StatefulWidget {
const ScriptTwoWidget ({super.key});
@override
State<ScriptTwoWidget> createState() => _ScriptTwoWidgetState();
}
class _ScriptTwoWidgetState extends State<ScriptTwoWidget> {
void listener() {
someCode();
setState(() {});
}
@override
void initState() {
ScriptOne.notifier.addListener(() {
listener();
});
someOtherCode();
super.initState();
}
@override
void dispose() {
ScriptOne.notifier.removeListener(() {
listener();
});
super.dispose();
}
[...]
}
given error when run:
setState() called after dispose(): _ScriptTwoWidgetState#f4bd6(lifecycle state: defunct, not mounted)
First tried without custom dispose function, then tried adding a custom dispose function as shown above, but it does not seem to change anything. Also ensured the function is not called anywhere else and also tried to remove listener by 200 iteration loop to ensure there is no possibility the function is called multiple times. Also I can't find anything referring this online. Is there some crucial thing I'm missing?
Thanks in advance!
Upvotes: 1
Views: 96