【发布时间】:2021-11-06 11:01:11
【问题描述】:
我为共享偏好创建了一个课程。类如下
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);
【问题讨论】:
标签: flutter flutter-sharedpreference