Joe
Joe

Reputation: 441

How to trigger a function at midnight

My app should trigger the function "dayChange()" whenever one of the two cases is true:

  1. Midnight has passed since the app was last active (opening or resuming)
  2. The app is active at midnight

"dayChange()" puts a new object in a list and saves that list to my instance of shared preferences.

What I need: I'm not aksing for a complete solution (hence no code), but some basic understanding and a direction to Google in.

Where I am at right now: I'm close to a solution for 1. I save DateTime.now() and load it via SharedPreferences everytime the app is opened. I then check if the day has changed. I'm not quite sure how to handle resuming yet, but I'm confident I can figure it out by googling life cycle stuff.

For 2 things get more complicated.

What are good approaches to this? I'm willing to dig deeper to solve the issue, but I don't want to go on a wild goose hunt without knowing a goose is what I actually need.

Upvotes: 3

Views: 1281

Answers (1)

jbryanh
jbryanh

Reputation: 2013

As documented here.

DateTime current = DateTime.now();
Stream timer = Stream.periodic( Duration(seconds: 1), (i) {
       current = current.add(Duration(seconds: 1));
      return current;
    });

    timers.listen((data)=> print(data));

Otherwise on open, just evaluate for date/time.

Upvotes: 2

Related Questions