我正在使用以下代码将值保存在 sharedPreferences
_onChanged(bool value) async {
sharedPreferences = await SharedPreferences.getInstance();
setState(() {
checkValue = value;
sharedPreferences.setBool("check", checkValue);
sharedPreferences.setString("username", username.text);
sharedPreferences.setString("password", password.text);
sharedPreferences.commit();
getCredential();
});
}
Run Code Online (Sandbox Code Playgroud)
但是在使用时我发现不推荐使用 commit 方法,那么它的替代品是什么?
你并不需要使用commit(),因为每一套(了setString,setBool等)执行承诺了。
在 iOS 上,同步已被弃用(这是commit()在 iOS 上持久化值的作用),因此commit()不再需要。
在 Android 上,对每个集合执行提交一直是默认行为,因此commit()在 Android 上是多余的。
总之,对于 Android 和 iOS 来说,只调用 set 方法应该没问题。
| 归档时间: |
|
| 查看次数: |
2982 次 |
| 最近记录: |