1

I am trying to implement a class where i can call SharedPreferences funtions.

import 'package:shared_preferences/shared_preferences.dart';


class UserPreferences {
  static SharedPreferences _preferences;

  static const _keyToken = 'token';

  static Future init() async {
    _preferences = await SharedPreferences.getInstance();
  }

  static Future setToken(String token) async =>
    await _preferences.setString(_keyToken, token);

  static String getToken() => _preferences.getString(_keyToken);

}

But i am getting following error:

The non-nullable variable '_preferences' must be initialized.
Try adding an initializer expression.

3 Answers3

2

Add late to the declaration of _preferences, like this:

static late SharedPreferences _preferences;

Final code:

import 'package:shared_preferences/shared_preferences.dart';

class UserPreferences {
  static late SharedPreferences _preferences;

  static const _keyToken = 'token';

  static Future init() async {
    _preferences = await SharedPreferences.getInstance();
  }

  static Future setToken(String token) async =>
      await _preferences.setString(_keyToken, token);

  static String getToken() => _preferences.getString(_keyToken);
}

0

You must create an object when you create your variable like below inside the method:

  static Future init() async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
  }

And for using like the property you can do it like below:

static SharedPreferences _preferences = SharedPreferences.getInstance();

And when you call this property you can use async/await for this _preferences property on that page.

Abbas Jafari
  • 1,492
  • 2
  • 16
  • 28
0

Understanding the Problem

The non-nullable variable '_preferences' must be initialized.

With NULL safety in place, you no longer can leave Non-Nullable types uninitialized.

 static SharedPreferences _preferences;

Here you have not initialized the non-nullable SharedPreferences.


Solution

1. Initialize it

 static Future init() async {
    SharedPreferences _preferences = await SharedPreferences.getInstance() as SharedPreferences;;
  }

2. Make it Nullable

NOTE : This solution will work but is not recommended as you are making it nullable which means it can hold null (can cause crashes in future program flow).

Add ? to make it "NULLABLE"

 static SharedPreferences? _preferences;
anirudh
  • 1,436
  • 5
  • 18
  • Initially I thought when I wrote this answer but then I found out that you used [my formatting signature](https://stackoverflow.com/a/67623408/6618622), haha. – CopsOnRoad Aug 05 '21 at 09:08
  • @CopsOnRoad hahaha.....But coincidentally even I came up with this format. I got to know today that you also follow this format :) – anirudh Aug 07 '21 at 08:22
  • 1
    I've been using this format for almost a year and you haven't seen any of my answers written this way (being a Flutter developer) is kinda strange! – CopsOnRoad Aug 07 '21 at 09:54
  • @CopsOnRoad yeah dude! Have you been a little inactive last month. Because most of my answers were posted last month and didn't see any of your answers – anirudh Aug 08 '21 at 06:53
  • Well, I don't really check all the "newly posted questions" now, although I used to do that a couple of years ago. Regardless, I've been posting answers on SO for almost 3 years now and most of my answers (which I am sure almost everyone active in "Flutter") must have seen them (probably including you, haha). So, my format is actually ~2-3 years old. Here's one such old unedited [post](https://stackoverflow.com/a/57132247/6618622) – CopsOnRoad Aug 08 '21 at 08:03
  • So you mean I copied yours ? Is that what you are trying to say ? If yes , I would like to say that our minds probably just worked the same to come up with this format. I never had a look at your answers before tbh – anirudh Aug 08 '21 at 12:58
  • All I was saying is your this line "I never had a look at your answers before tbh" doesn't sound that true because as I mentioned earlier, almost my all answers have that format and the chances of you not seeing any of my posts is almost zero. – CopsOnRoad Aug 08 '21 at 14:39