【发布时间】:2021-04-21 19:03:04
【问题描述】:
我创建了一个单独的 calss 页面来处理来自所有不同应用程序页面的共享首选项。保存或编辑数据。我可以轻松保存字符串数据,但我在保存 bool 类型的数据时遇到了问题。我尝试保存 bool 类型的数据来存储用户是否登录的状态。找了半天没找到解决办法。
完整代码:
import 'package:shared_preferences/shared_preferences.dart';
class MyPreferences {
static const ID = "id";
static const STATE = "state";
static final MyPreferences instance = MyPreferences._internal();
static SharedPreferences _sharedPreferences;
String id = "";
String state = "";
MyPreferences._internal() {}
factory MyPreferences() => instance;
Future<SharedPreferences> get preferences async {
if (_sharedPreferences != null) {
return _sharedPreferences;
} else {
_sharedPreferences = await SharedPreferences.getInstance();
state = _sharedPreferences.getString(STATE);
id = _sharedPreferences.getString(ID);
return _sharedPreferences;
}
}
Future<bool> commit() async {
await _sharedPreferences.setString(STATE, state);
await _sharedPreferences.setString(ID, id);
}
Future<MyPreferences> init() async {
_sharedPreferences = await preferences;
return this;
}
}
谁能帮我制作布尔数据。
谢谢
【问题讨论】:
标签: flutter