2

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.

Simi
  • 21
  • 1
  • 2
  • `Future.delayed(Duration.zero, () {ref.read(disconnectString.notifier).change('default string value');});` in dispose also not work? – Ruble Dec 06 '22 at 17:08
  • No, that does not work. I still get "Looking up a deactivated widget's ancestor is unsafe" If i wrap it in a zero duration future delay. – Simi Jan 02 '23 at 17:57
  • Can you provide a minimally reproducible copy-run example? – Ruble Jan 03 '23 at 08:35

0 Answers0