【发布时间】:2020-10-03 01:20:20
【问题描述】:
我是新手,刚开始编码。我正在使用颤振制作一个简单的应用程序,它每天都会打印一个新报价,我为此使用共享首选项,它需要两次热重启或两次打开应用程序来更新值。我不确定它为什么会发生,请你给我一些启发。这是我的代码:
Future<int> setDay(n) async {
final pref = await SharedPreferences.getInstance();
pref.setInt('day', n);
}
Future<String> setQuote() async {
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setString('quote', fromallquotes['$randomnumber']);
}
Future<String> changequote() async {
final pref = await SharedPreferences.getInstance();
var currentday = pref.getInt('day');
var quote = pref.getString('quote');
if (quote == null) {
todaysquote = fromallquotes["$randomnumber"];
}
if (currentday == DateTime.now().weekday) {
todaysquote = quote;
} else {
setQuote();
todaysquote = quote;
setDay(DateTime.now().weekday);
}
return todaysquote;
}
【问题讨论】: