如何解决将计时器字符串保存到共享首选项
我目前正在制作一个计时器应用程序,该应用程序可以在水龙头上启动,然后停止并在上滑面板中的另一水龙头上列出时间。我有一个获取计时器字符串的类,它看起来像这样
class Dependencies {
final Stopwatch stopwatch = new Stopwatch();
CurrentTime currentTime;
List<String> savedTime = List<String>();
transformTimeToString(int milliseconds) {
currentTime = transformTimeToTime(stopwatch.elapsedMilliseconds);
int hundreds = (milliseconds / 10).truncate();
int seconds = (hundreds / 100).truncate();
int minutes = (seconds / 60).truncate();
String hundredsStr = (hundreds % 100).toString().padLeft(2,'0');
String secondsStr = (seconds % 60).toString();
String secondsMinStr = (seconds % 60).toString().padLeft(2,'0');
String minutesStr = (minutes % 60).toString();
if (currentTime.minutes > 0) {
return '$minutesStr:$secondsMinStr.$hundredsStr';
} else {
return '$secondsStr.$hundredsStr';
}
}
transformTimeToTime(int milliseconds) {
int hundreds = (milliseconds / 10).truncate();
int seconds = (hundreds / 100).truncate();
int minutes = (seconds / 60).truncate();
return CurrentTime(
hundreds: hundreds % 100,seconds: seconds % 60,minutes: minutes % 60,);
}
}
所以我在将该字符串列在json文件上以将其保存到共享首选项时遇到麻烦。如果一个有很好理解的人可以将我的代码推向正确的方向,那将非常有帮助。谢谢猎人
这里是所有Timer Page,Timer String,Whole project
的链接解决方法
您的问题尚不清楚,所以我假设您想将List<String> savedTime;
保存到SharePreference并以某种方式重新加载。
saveTimeList(String key,List<String> savedTime) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList(key,savedTime);
}
这样检索:
Future<Set<String>> loadSavedTime(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> stringValue = prefs.getStringList(key) ?? [];
#Input your additional code to convert String back to Time if you want
#
return stringValue.toSet();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。