Flutter 如何使用SharedReference保存2个变量?
我尝试构建一个应用程序,它应该使用Flutter 如何使用SharedReference保存2个变量?,flutter,dart,sharedpreferences,Flutter,Dart,Sharedpreferences,我尝试构建一个应用程序,它应该使用sharedPreferences保存两个变量,而不必两次编写sharedPreferences代码。因此,我在下面编写了代码,并使用变量nameKey作为保存值的位置,使用safeVariable作为保存值。我有两个不同的变量需要保存,counter1(按下按钮时值+1)和counter2(按下按钮时值-1)。我的问题是两个变量都变小了一个,但只有一个变量counter2应该这样做。有人在我的代码中发现了错误吗? 这就是代码: class _MyHomePag
sharedPreferences
保存两个变量,而不必两次编写sharedPreferences
代码。因此,我在下面编写了代码,并使用变量nameKey
作为保存值的位置,使用safeVariable
作为保存值。我有两个不同的变量需要保存,counter1
(按下按钮时值+1)和counter2
(按下按钮时值-1)。我的问题是两个变量都变小了一个,但只有一个变量counter2
应该这样做。有人在我的代码中发现了错误吗?
这就是代码:
class _MyHomePageState extends State<MyHomePage> {
int counter1 = 0;
int counter2 = 20;
String nameKey = "eins";
int safeVariable;
void _incrementCounter() {
setState(
() {
counter1++;
counter2--;
nameKey = "eins";
safeVariable = counter1;
save();
nameKey = "zwei";
safeVariable = counter2;
save();
},
);
}
@override
void initState() {
super.initState();
}
Future<bool> save() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return await preferences.setInt(nameKey, safeVariable);
}
Future<int> load() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getInt(nameKey);
}
set() {
load().then((value) {
setState(() {
safeVariable = value;
});
});
}
@override
Widget build(BuildContext context) {
nameKey = "eins";
set();
counter1 = safeVariable;
nameKey = "zwei";
set();
counter2 = safeVariable;
return Scaffold(
appBar: AppBar(
title: Text("Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(counter1.toString(),
style: Theme.of(context).textTheme.headline4),
Text(counter2.toString(),
style: Theme.of(context).textTheme.headline4),
FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
),
]),
),
);
}
}
class\u MyHomePageState扩展状态{
int计数器1=0;
int计数器2=20;
字符串nameKey=“eins”;
int安全变量;
void _incrementCounter(){
设定状态(
() {
计数器1++;
计数器2--;
nameKey=“eins”;
safeVariable=counter1;
save();
nameKey=“zwei”;
safeVariable=counter2;
save();
},
);
}
@凌驾
void initState(){
super.initState();
}
Future save()异步{
SharedReferences首选项=等待SharedReferences.getInstance();
return wait preferences.setInt(nameKey,safeVariable);
}
Future load()异步{
SharedReferences首选项=等待SharedReferences.getInstance();
返回preferences.getInt(nameKey);
}
集合(){
加载()。然后((值){
设置状态(){
安全变量=值;
});
});
}
@凌驾
小部件构建(构建上下文){
nameKey=“eins”;
set();
计数器1=安全变量;
nameKey=“zwei”;
set();
计数器2=安全变量;
返回脚手架(
appBar:appBar(
标题:文本(“测试”),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
Text(counter1.toString(),
风格:Theme.of(context.textTheme.headline4),
Text(counter2.toString(),
风格:Theme.of(context.textTheme.headline4),
浮动操作按钮(
按下时:\ u递增计数器,
子:图标(Icons.add),
),
]),
),
);
}
}
您必须等待save()操作结束,如下所示:
在:
“等待”:
如果要同时在SharedReferences中存储多个值,则只能加载/保存字符串列表(列表)。
这是如何做到的:
prefs.setStringList("key", ["a","b","c"]);
如果有int,则需要将每个int转换为字符串
List<String> mylist = ["$counter1","$counter2"];
List mylist=[“$counter1”、“$counter2”];
您正在为counter1=安全变量赋值;
prefs.setStringList("key", ["a","b","c"]);
List<String> mylist = ["$counter1","$counter2"];