Flutter: Detect a new day

anoop m
1 min readAug 16, 2020

I recently had a need to show a particular screen from home page of my app .. every day only once, for that I had to determine if it was a new day and below is what I have done.

1. Save the date on which the particular screen is shown first.
— When the screen is shown, capture the date and save it to shared preferences
— Use shared preference package for that.
— Convert to UTC and save

 DateTime.now().toUtc().toIso8601String()

2. When user opens the app again or re-opens the base screen
- Track the life cycle events
- Fetch the date from preferences.
- Get the date and covert to local time

DateTime dateTime = DateTime.parse(date).toLocal();

3. Now get the current date

int currentDay = DateTime.now().day;

4. Compare the day

// dateTime is the date fectched from preferences
if (dateTime.day != currentDay)
// Now its a new day
}

anoop4real

--

--