I wanted to keep the time data stored in the mobile localcaly so when ever the app is closed and open backed i want it to be shown. that part works but when ever the button is pressed to set new time it wont update to the newly set sharedprefence to show instead of the load or the new time.
This is the function that runs every time the button is being pressed
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();
}
This is the function for the Future builder
Future <String> timeShowtimein()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return Future.delayed(Duration(seconds: 1),()async{
return await sharedPreferences.getString("timeIn");
});
}
And here is the UI builder
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))
],
)
);
}