Reputation: 3296
I am writing a Flutter app that turns a light on and off based on a time of day. So let's say the time range is to turn on at 5pm and off at 6pm. If the app is opened during this time or if the app is already opened when the time hits, the light would turn on.
I already have the light widget all worked out and will be using opacity to simulate the light on and off.
Container(
height: MediaQuery.of(context).size.height * 0.18,
decoration: new BoxDecoration(
color: Colors.red.withOpacity(0.1),
shape: BoxShape.circle,
),
),
I have been doing some research on Dart's DateTime, but I am not really sure where to start because I want the light to turn on if the app is opened during the "on" window. But also, if the app is already open before the "on" window it will poll in the background and if the "on" window hits, the light will turn on.
Upvotes: 5
Views: 5537
Reputation: 1114
I had a similar condition for my app, where I had to trigger an event at a certain time in a day.
For that first I created a DateTime instance and saved it on Firestore. You can save that DateTime instance on local Database also, eg:SQFlite etc.
//DateTime instance with a specific date and time-
DateTime atFiveInEvening;
//this should be a correctly formatted string, which complies with a subset of ISO 8601
atFiveInEvening= DateTime.parse("2021-08-02 17:00:00Z");
//Or a time after 3 hours from now
DateTime threehoursFromNow;
threeHoursFromNow = DateTime.now().add(Duration(hours: 3));
Now save this instance to FireStore with an ID-
saveTimeToFireStore() async {
await FirebaseFirestore.instance.collection('users').doc('Z0ZuoW8npwuvmBzmF0Wt').set({
'atFiveInEvening':atFiveInEvening,
});
}
Now retrieve this set time from Firestore when the app opens-
getTheTimeToTriggerEvent() async {
final DocumentSnapshot doc =
await FirebaseFirestore.instance.collection('users').doc('Z0ZuoW8npwuvmBzmF0Wt').get();
timeToTriggerEvent= doc['atFiveInEvening'].toDate();
//Now use If/Else statement to know, if the current time is same as/or after the
//time set for trigger, then trigger the event,
if(DateTime.now().isAfter(timeToTriggerEvent)) {
//Trigger the event which you want to trigger.
}
}
But here we'll have to run the function getTheTimeToTriggerEvent() again and again to check if the time has come.
Upvotes: 0
Reputation: 1595
Dart does have a few primitives that can be used to trigger events based on time as mentioned in this post: Does Dart have a scheduler?
When the app is open, using Stream.periodic()
or possibly a package like cron for dart could help you achieve the desired outcome.
The tricky part is related to background execution. Android is much more liberal when it comes to executing background tasks, whereas iOS really locks down what you're able to do.
For Android, the Flutter team actually wrote a package called Android Alarm Manager, which takes advantage of the Android Alarm Manager service to execute Dart code when an alarm fires.
As far as I know, there isn't an exact equivalent for iOS. There's actually an entire thread about this on the Flutter Github. However, there is a package called background_fetch that allows you to take advantage of background execution on both platforms. During this time you could check a local database to see if a light has been scheduled to be turned on during that time and make the necessary call. Unfortunately, this isn't a precise mechanism so lights may not be turned on at the exact time that you'd like.
If it's an option, it would be much better to schedule these tasks server-side so that you have more fine-grained control over scheduling via cron or something similar.
Upvotes: 6