如何取消CUBIT旧功能调用?

发布于 2025-01-19 18:37:42 字数 699 浏览 1 评论 0原文

我试图在扑朔迷离的项目中使用cubit,但是我正面临这个问题,我试图用未来的返回类型来调用其功能,以便需要完成它的时间,但与此同时,我收到了通知在此CUBIT附带的另一个功能,但我想首先取消以前的功能,我该如何执行此操作,感谢您的帮助。

class ConnectionCheckerCubit extends Cubit<ConnectionCheckerState> {
  ConnectionCheckerCubit() : super(ConnectionCheckerInitial());

  void whenConnectedFunction() {
    print('test1');
  }

  void whenDisconnectedFunction() async {
    print('test2');
    await Future.delayed(Duration(seconds: 10));
    print('test3');
  }
}

我称第二个函数 whendisconnectedFunction 它的print test2 ,然后我将上部函数 当ConnectedFunction 在10秒结束之前,它的打印 test1 ,但它也打印了 test3 我如何解决此问题。谢谢 。

I am trying to use cubit in my flutter project but i am facing this problem i am trying to call a function its function with future return type so its need time to be done but at the same time i got a notify so i need to call another function in side this cubit but i want to cancel the previous function first how can i do this thanks for your help .

class ConnectionCheckerCubit extends Cubit<ConnectionCheckerState> {
  ConnectionCheckerCubit() : super(ConnectionCheckerInitial());

  void whenConnectedFunction() {
    print('test1');
  }

  void whenDisconnectedFunction() async {
    print('test2');
    await Future.delayed(Duration(seconds: 10));
    print('test3');
  }
}

i call the second function whenDisconnectedFunction its print test2 then i call the upper function whenConnectedFunction before the 10 seconds end its print test1 but its also test3 is printed how can i fix this problem . thx .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2025-01-26 18:37:42

一旦函数被调用,您就无法取消它,但是您可以做的是在 future 完成后放置一个检查器来检查函数是否需要继续前进。

我写了一个示例代码来帮助您更好地理解这一点,您可以粘贴此 dartpad 亲自尝试。

void main() async {
  
  print("Testing without cancel");
  final cubitWithoutCancel = ConnectionCheckerCubit();
  cubitWithoutCancel.whenDisconnectedFunction();
  cubitWithoutCancel.whenConnectedFunctionWithoutCancel();
  
  await Future.delayed(const Duration(seconds: 11));
  
  print("Testing with cancel");
  final cubit = ConnectionCheckerCubit();
  cubit.whenDisconnectedFunction();
  cubit.whenConnectedFunction();
}


class ConnectionCheckerCubit {
  ConnectionCheckerCubit();
  
  bool _cancelFunctionIfRunning = false;

  void whenConnectedFunction() {
    _cancelFunctionIfRunning = true;
    print('test1');
  }
  
  void whenConnectedFunctionWithoutCancel(){
    print('test1');
  }

  void whenDisconnectedFunction() async {
    print('test2');
    await Future.delayed(Duration(seconds: 10));
    if(_cancelFunctionIfRunning) return;
    print('test3');
  }
}

上面代码的输出如下:

Testing without cancel
test2
test1
test3
Testing with cancel
test2
test1

这是一个相当简单的实现,可以帮助您理解如何基于外部因素中途退出函数的概念,您可以创建一个函数来为您完成此任务,而不是手动更改一个布尔值。

You can't cancel a function once it's called, but what you can do is put a checker once a future is complete to check if the function needs to proceed forward or not.

I have written an example code to help you understand this better, you can paste this is dartpad to try for yourself.

void main() async {
  
  print("Testing without cancel");
  final cubitWithoutCancel = ConnectionCheckerCubit();
  cubitWithoutCancel.whenDisconnectedFunction();
  cubitWithoutCancel.whenConnectedFunctionWithoutCancel();
  
  await Future.delayed(const Duration(seconds: 11));
  
  print("Testing with cancel");
  final cubit = ConnectionCheckerCubit();
  cubit.whenDisconnectedFunction();
  cubit.whenConnectedFunction();
}


class ConnectionCheckerCubit {
  ConnectionCheckerCubit();
  
  bool _cancelFunctionIfRunning = false;

  void whenConnectedFunction() {
    _cancelFunctionIfRunning = true;
    print('test1');
  }
  
  void whenConnectedFunctionWithoutCancel(){
    print('test1');
  }

  void whenDisconnectedFunction() async {
    print('test2');
    await Future.delayed(Duration(seconds: 10));
    if(_cancelFunctionIfRunning) return;
    print('test3');
  }
}

The output of the above code is as follows:

Testing without cancel
test2
test1
test3
Testing with cancel
test2
test1

This is a rather simple implementation to help you understand the concept of how to quit a function midway based on an external factor, you can create a function to do this task for you instead of manually changing a boolean.

゛时过境迁 2025-01-26 18:37:42

我找到了解决此问题的好方法,您可以使用Bloc_concurrency软件包使用BLOC,此软件包让您根据需要处理BLOC事件,因此,如果您设置了 RECTARTABLE的事件开始新的。

I find a good way to solve this problem you can use bloc with bloc_concurrency package, this package let you handle bloc event as you want, so if you set the event for restartable it will cancel the old event and start new one .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文