I have an Api call. I want to call the Api again when 24 hours passed from the previous Api call. How to find the time difference in flutter. please explain your answer in detail.
Support:
Official docs 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 docshttps://api.dart.dev/stable/2.8.3/dart-core/Duration-class.html
Example: (Edit)
Save date time in SharedPreferences on API call
sharedPrefs.putInt('apiCallTime',DateTime.now().milliSecondsSinceEpoch);
When you want to recall the API, get time and call
int lastCallTimeInSeconds = sharedPrefs.getInt('apiCallTime')??DateTime.now().milliSecondsSinceEpoch;
DateTime lastCallTime = DateTime.fromMilliSecondsSinceEpoch(lastCallTimeInSeconds);
if(DateTime.now().difference(lastCallTime).inHours>=24){
//Re call API here...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.