Flutter Firebase refresh document snapshot every 24h

Viewed 22

I have a recipe collection with 100 entries in firebase firestore. The idea is that the user only sees 3 different recipes in my app. Those 3 recipes need to change every 24h. So on Monday the user sees 3 (different) recipes from my collection, on Tuesday the next 3 (randomly chosen) recipes and so on. How is that possible to achieve?

1 Answers

Simply store another field called 'lastRefresh' in a document for each user.

The value of that field will be a DateTime, of course you have to format it to a string with DateTime().toIso8601String() to store it on Firestore.

Then on app start get the value and parse it to DateTime again with DateTime.parse(value_from_firestore) and get the difference to now. You can achieve this by calling DateTime.now().difference(parsed_date_time_from_firestore) which will return a duration.

You can check wheter this duration is greater then 24 hours. If it is, load another 3 recipes from firestore and set lastRefresh with DateTime.now().toIso8601String() to now again.

Related