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.
Asked
Active
Viewed 575 times
1 Answers
3
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 docs https://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...
}

Naveen Avidi
- 3,004
- 1
- 11
- 23