0

I am using a very simple class as a SharedPrefs handler in my futter app,

class SharedPrefsHandler {
  SharedPreferences? _sharedPrefs;

  SharedPrefsHandler() {
    _sharedPrefs ?? _getSharedPrefs();
  }

  Future<void> _getSharedPrefs() async {
    _sharedPrefs = await SharedPreferences.getInstance();
  }

  Future<bool> saveStringToPrefs(String key, String value) {
    print(" -- saveStringToPrefs --> Key: $key, Value: $value");
    return _sharedPrefs!.setString(key, value);
  }

}

I am instantiating it using the get_it package, right at the begining of my app, on the main.dart.

I keep gettig this annoying error, and I am not sure if I am doing something wrong here...

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value

My flutter doctor shows I am on stable channel:

Flutter (Channel stable, 2.2.2, on Mac OS X .....

codeKiller
  • 5,493
  • 17
  • 60
  • 115

1 Answers1

0
_sharedPrefs ?? _getSharedPrefs();

should be

_sharedPrefs ??= _getSharedPrefs();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440