简体   繁体   English

Flutter(飞镖)的时差

[英]Time Difference in Flutter (Dart)

I have an Api call.我有一个 Api 电话。 I want to call the Api again when 24 hours passed from the previous Api call.我想在上一次 Api 呼叫过去 24 小时后再次呼叫 Api。 How to find the time difference in flutter.如何找到 flutter 中的时差。 please explain your answer in detail.请详细解释您的答案。

Support:支持:

Official docs https://api.dart.dev/stable/2.8.3/dart-core/DateTime/difference.html官方文档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 has more properties and methods to check more details. Duration 有更多的属性和方法来检查更多的细节。 Duration docshttps://api.dart.dev/stable/2.8.3/dart-core/Duration-class.html持续时间文档https://api.dart.dev/stable/2.8.3/dart-core/Duration-class.html

Example: (Edit)示例:(编辑)

Save date time in SharedPreferences on API call在 API 调用的 SharedPreferences 中保存日期时间

sharedPrefs.putInt('apiCallTime',DateTime.now().milliSecondsSinceEpoch);

When you want to recall the API, get time and call当您想召回 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM