'Notifying listeners during widget disposing

If I have a class with ChangeNotifier :

class MyClass extends ChangeNotifier {
    int myValue = 0;
    incrementsValue(){
        myValue ++;
        NotifyListeners();
    }
}

A widget listening to it :

@override
Widget build(BuildContext context){
    return Text('${context.watch<MyClass>().myValue}');
}

And a need to call incrementsValue() during widget dispose :

@override
dipose(){
    myClass.incrementsValue();
}

I get this error :

This _InheritedProviderScope<MyClass?> widget cannot be marked as needing to build because the framework is locked.

Is there a way to avoid this error ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source