• [SafetyTime] is a timelock which designed to block multiple calls within an interval. It works like a synchronized mutex ([lock]and[unlock]).[SafetyTime] is suitable for flutter. It avoids Nested and state management.

  • [SafetyTime] provides two cor functions:

    • The first is to block users from repeated clicks, repeated network requests, etc.For example, in a multi-select list, the user touches multiple options at the same time.As another example, when the network requests, the user clicks again to request. Specifically, [SafetyTime] will compare the interval between two times,and if it is less than the safe interval [SafetyTime], the event will be discarded.
    • The second is a pair of methods similar to [lock] and [unlock], they are [tryLockForever] and [unlockForever]. Specifically, [SafetyTime] will lock a indefinitely [key] until the user manually calls [unlockForever] to unlock the lock.

Getting started

Add dependency

You can use the command to add dio as a dependency with the latest stable version:

$ dart pub add flutter_safety_time

Or you can manually add dio into the dependencies section in your pubspec.yaml:



The user taped multiple buttons before the new page was pushed.

onATap: {
  if(SafetyTime.unavailable) return;
  Navigator.push(context, PageA());

onBTap: {
  if(SafetyTime.unavailable) return;
  Navigator.push(context, PageB());

Limit onece login in 1 minute.

loginRequest() async {
    if(SafetyTime.unavailableOf('Login', Duration(seconds: 1))) {
        alert('Limit onece login in 1 minute.');
    await Login();

[SafetyTime.tryLockForever] can lock [key] for a long time. When [key] is locked, both [unavailable] and [unavailableOf] return true, but [synchronizedKey] is not affected.

updateUserInfo() async {
    if(SafetyTime.tryLockForever('UpdateUserInfo')) {
      await doSomething();

InPageA {

InPageB {

Synchronous key Executes [computation] when lock is available. Only one asynchronous block can run while the key is retained. If [timeout] is specified, it will try to grab the lock and will not call the computation callback and throw a [TimeoutExpection] is the lock cannot be grabbed in the given duration.

save(x) {
  await SafetyTime.synchronizedKey('WritToFile', (userInfo) async {

InPageA() {
  ... ...
  ... ...

InPageB() {
  ... ...
  ... ...

Additional information

  • 2023


View Github