【发布时间】:2023-03-30 12:23:02
【问题描述】:
我想将时间数据保存在移动本地卡里,这样当应用程序关闭和打开时,我希望它显示出来。该部分有效,但是当按下按钮设置新时间时,它不会更新为新设置的 sharedprefence 以显示而不是加载或新时间。
这是每次按下按钮时运行的函数
void timeInPush()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String now =await new DateFormat.yMd().add_Hm().format(new DateTime.now());
String day =await new DateFormat.d().add_Hm().format(new DateTime.now());
sharedPreferences.setString("timeIn", now);
sharedPreferences.setString("timeOut",null);
sharedPreferences.reload();
}
这是未来构建器的功能
Future <String> timeShowtimein()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return Future.delayed(Duration(seconds: 1),()async{
return await sharedPreferences.getString("timeIn");
});
}
这是 UI 构建器
Container timeText(){
return Container(
child:Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FutureBuilder(
future:timeShowtimein() ,
builder: (context, snapshot){
if(snapshot.hasData){
return Text("${snapshot.data}");
}else{
return Text("Loading");
}
}),
SizedBox(
width:20,
),
Text("$Timeout",style:TextStyle(
color: Colors.redAccent,
fontSize: 15.0))
],
)
);
}
【问题讨论】:
标签: flutter mobile sharedpreferences flutter-futurebuilder