我有一个 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...
}
关于android - Flutter 中的时差 (Dart),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62080784/