setFakeNow function
Set a fake time that now will return. Set to null to return to real time again.
If persist is set to true,
it is persisted between restarts by saving the time to LocalConfig.
If elapse is set to true, the time returned from now
will be fakeNow plus the elapsed time since this function
was executed.
This allows for syncing time to say, a remote server.
Warning:
Using elapse set to true in combination with persist set to true
will result in only fakeNow being persisted, causing the value
loaded on app restart to be exactly fakeNow and not any time that
has elapsed after it was saved.
Implementation
void setFakeNow(DateTime? fakeNow, {bool persist = true, bool elapse = false}) {
_now = fakeNow;
if (elapse) {
_fakeTimeElapsedSince
..reset()
..start();
} else {
_fakeTimeElapsedSince.stop();
}
if (persist) {
if (fakeNow == null) {
getApp().localConfig.reset(_kFakeNowLocalConfigKey);
} else {
getApp().localConfig.setString(
_kFakeNowLocalConfigKey,
fakeNow.toUTCIso8601StringNoMSUS(),
);
}
}
_fakeNowStreamController.add(fakeNow);
}