我正在尝试实现一个可以调用 SharedPreferences 函数的类。
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);
}
但我收到以下错误:
The non-nullable variable '_preferences' must be initialized.
Try adding an initializer expression.