Ick*_*sir 6 settings user-interface dart flutter
我想知道如何在 Flutter 中本地保存数据。我正在我的应用程序中创建一个“设置”菜单,并且我有一个由该 UI 制成的草稿,但我想在用户关闭该菜单时保存用户的首选项,但我不知道如何实现这一点。
你知道一些教程吗?我在Youtube上搜索过,但不知道如何搜索。我只找到了 UI 教程,但我不想要那个。
我的用户界面图片是(我想保存该布尔选项)。
我将不胜感激您能给我的任何帮助!
你应该使用shared_preferences
包。这非常简单,而且适用于像这样的非关键数据!这是如何使用它的示例:
class SettingsRepository {
final SharedPreferences preferences;
SettingsRepositoryImpl(this.preferences);
Future<int> getDifficulty() {
return preferences.getInt(Keys.difficulty) ?? 1;
// this 1 in the end is a default value
}
Future setDifficulty(int difficulty) {
return preferences.setInt(Keys.difficulty, difficulty);
}
}
Run Code Online (Sandbox Code Playgroud)
要了解更多信息,请访问https://pub.dev/packages/shared_preferences。我假设你想打电话preferences.setBool
您可以使用共享首选项保存布尔选项。
Future<Null> saveOption(bool isSelected) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('option', isSelected);
}
Run Code Online (Sandbox Code Playgroud)
然后您可以从那里获得该选项。
Future<bool> getOption() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('option');
}
Run Code Online (Sandbox Code Playgroud)
您可以从这里阅读更多内容。
归档时间: |
|
查看次数: |
5276 次 |
最近记录: |