【问题标题】:Flutter SharedPreferences Null check operator used on a null valueFlutter SharedPreferences Null 检查运算符用于空值
【发布时间】: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


    【解决方案1】:

    在调用_preferences!.之前,将此行也添加到您的打印行下

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

    【讨论】:

    • 但我已经在 getInstancefction 中进行了空检查
    • 虽然你没有打电话。试试看吧。
    【解决方案2】:

    尝试添加此WidgetsFlutterBinding.ensureInitialized(); 如果不存在,则在 main.dart 文件中的非常 first line of you main() 方法中。

    这里的问题是

    1. 该类有一个静态函数,负责初始化变量和can be accessed without an object of class StorageUtil
    2. nonstatic function 被称为need to create an object of StorageUtil class,然后访问该函数,由于static variables are not initialized which are initialized in the static function hence null

    从代码的 sn-p 看来,您愿意创建一个单例类,这里是它的正确代码:

    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);
      }
    }
    

    您想在哪里使用首选项只需调用:

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

    这是整个应用程序中唯一存在的对象。

    或者

    如果您不想更改您的课程,那么只需将其添加到顶部使用 _preferences 的所有 nonstatic functions

    并且还要添加这个空检查

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

    因为您可能有多个 StorageUtil 实例,每次都使 _preferences 变量为空。

    【讨论】:

    • 但我已经在 getInstancefction 中进行了空检查
    • 您在 getInstanceFunction 中创建它,但您的函数是静态的,因此它不会存储 _preference 它
    • 用于下一次调用另一个函数
    猜你喜欢
    • 2021-06-17
    • 2021-12-31
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2023-01-05
    • 2022-06-15
    • 2022-01-05
    相关资源
    最近更新 更多