I have the following Riverpod StateNotifierProvider:
class DisconnectString extends StateNotifier<String> {
DisconnectString() : super('default string value');
void change(String text) => state = text;
}
final disconnectString = StateNotifierProvider<DisconnectString, String>(
(ref) => DisconnectString());
Depending on the current screen, the disconnect string should be different. Currently I'm able to change the string when entering a new screen in the initState method:
@override
void initState() {
Future.delayed(Duration.zero, () {
ref.read(disconnectString.notifier).change('A new string value');
});
}
However, I'm not able to set it back to the default value in the dispose method.
@override
void dispose() {
ref.read(disconnectString.notifier).change('default string value');
}
I just get "Looking up a deactivated widget's ancestor is unsafe"
I could not find any stackOverflow Riverpod issues regarding this. However, I tried all the solutions in this ticket Calling a method with Provider.of(context) inside of dispose() method cause "Looking up a deactivated widget's ancestor is unsafe.", without success.
For example I tried to store the Reader (ref.read) in a variable and use that in the dispose method, but it didn't work.