The non-nullable variable ‘_instance‘ must be initialized.

609 阅读1分钟
static  SharedPreferences _storage;//会报错

_initSP() async{
  _storage ??= await SharedPreferences.getInstance();
}

改成

static late SharedPreferences _storage;
_initSP() async{
  _storage ??= await SharedPreferences.getInstance();
}
在Dart 2.12中添加了late关键字,他的作用:
  • 显式声明一个非空的变量,但不初始化
  • 延迟初始化变量