I am looking at the docs of Stopwatch and I'm sure that they don't have a method to start the stopwatch with the initial value.
I am developing an app that requires measuring the elapsed time. Therefore, Stopwatch becomes the obvious choice here. However, there is a use case, where the users of the app may accidentally close the app when clearing the background apps.
Since, running headless dart code in the background is kind of vague right now, I believe it is of best interest to keep track of time and the time gap, if there is any when resuming the app after an accidental close. A separate data object like the following could keep track of time and whether the stopwatch is running...
class StopwatchTracker{
final stopwatch;
final lastUpdated;
final isRunning;
final systemTime;
StopwatchTracker({this.stopwatch, this.lastUpdated, this.isRunning, this.systemTime});
}
With this I have an object that has data about the lastUpdated time from the stopwatch.
Compare this to the systemTime, which will be the device's current system time.
Now, we can see if there is a gap between the lastUpdated time and the systemTime. If there is a gap, the stopwatch should "leap" to the time, by "gap" units.
This StopwatchTracker object will only be initialized on app start/resume and every few seconds, it will update the lastUpdated time. I think the logic is there, but, as I mentioned, the Stopwatch class in dart, doesn't have a method to initialize it with a starting value.
I'm wondering whether I could extend the Stopwatch class to house a method to do that. Or a second option will be to update the ellapsedMillis itself or add the gap in mills to the ellapsedMillis and then show the result on the screen.
Will love to hear from you guys on this!