Flutter SharedPreferences Null check operator used on a null value

Issue

I’ve made a class for shared preferences. The class is as follows

class StorageUtil {
  static StorageUtil? _storageInstance;
  static SharedPreferences? _preferences;

  static Future<StorageUtil?> getInstance() async {
    if (_storageInstance == null) {
      _storageInstance = StorageUtil();
    }
    if (_preferences == null) {
      _preferences = await SharedPreferences.getInstance();
    }
    return _storageInstance;
  }

  addStringtoSF(String key, String value) async {
    print(' inside sharedPreferences file $key $value'); // Receives data here
    await _preferences!.setString(key,
        value); //Unhandled Exception: Null check operator used on a null value
  }

When ever i try to store the values i’m getting a error ‘Null check operator used on a null value’

This is how i’m passing down the values to the store function. I’m receiving the data inside the function. But cannot store the values inside it. What causes this?

String? userResponse = json.encode(authResponse);
      print('This is userResponse type');
      _storageUtil.addStringtoSF('userData', userResponse);

Solution

Try adding this WidgetsFlutterBinding.ensureInitialized();
in the very first line of you main() method in main.dart file if not present.

The problem over here is that

  1. The class has a static function that is responsible for the initialization of variables and can be accessed without an object of class StorageUtil.
  2. When the nonstatic function is called you need to create an object of StorageUtil class and then access that function due to which the static variables are not initialized which are initialized in the static function hence null.

From the snippet of code it seems you are willing to make a singleton class here is the correct code for it:

class StorageUtil {
  static StorageUtil storageInstance = StorageUtil._instance();
  static SharedPreferences? _preferences;

  StorageUtil._instance(){
    getPreferences();
  }
  void getPreferences()async{
    _preferences = await SharedPreferences.getInstance();
  }


  addStringtoSF(String key, String value) async {
    print(' inside sharedPreferences file $key $value'); // Receives data here
    await _preferences!.setString(key,
        value);
  }
}

Where ever you want the preference to be used just call:

final StorageUtil storage = StorageUtil.storageInstance;
storage.AnyNonStaticFunctionName()// call for methods in the StorageUtil Class

this is the one and only object that will exist throughout the application.

Or

if you don’t want to change your class then just add this in all the nonstatic functions at the top which uses _preferences

And also add this null check

if (_preferences == null) {
    _preferences = await SharedPreferences.getInstance();
}

because you may be having multiple instances of the StorageUtil making the _preferences variable null each time.

Answered By – ritik kumar srivastava

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *