[英]Time Difference in Flutter (Dart)
我有一个 Api 电话。 我想在上一次 Api 呼叫过去 24 小时后再次呼叫 Api。 如何找到 flutter 中的时差。 请详细解释您的答案。
支持:
官方文档https://api.dart.dev/stable/2.8.3/dart-core/DateTime/difference.html
var berlinWallFell = new DateTime.utc(1989, DateTime.november, 9);
var dDay = new DateTime.utc(1944, DateTime.june, 6);
Duration difference = berlinWallFell.difference(dDay);
assert(difference.inDays == 16592);
Duration 有更多的属性和方法来检查更多的细节。 持续时间文档https://api.dart.dev/stable/2.8.3/dart-core/Duration-class.html
示例:(编辑)
在 API 调用的 SharedPreferences 中保存日期时间
sharedPrefs.putInt('apiCallTime',DateTime.now().milliSecondsSinceEpoch);
当您想召回 API 时,获取时间并致电
int lastCallTimeInSeconds = sharedPrefs.getInt('apiCallTime')??DateTime.now().milliSecondsSinceEpoch;
DateTime lastCallTime = DateTime.fromMilliSecondsSinceEpoch(lastCallTimeInSeconds);
if(DateTime.now().difference(lastCallTime).inHours>=24){
//Re call API here...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.