我为共享偏好开设了一个课程。班级如下
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
}
每当我尝试存储值时,都会收到错误消息“空值检查运算符用于空值”
这就是我将值传递给 store 函数的方式。我正在接收函数内的数据。但不能将值存储在其中。这是什么原因造成的?
String? userResponse = json.encode(authResponse);
print('This is userResponse type');
_storageUtil.addStringtoSF('userData', userResponse);