如何取消CUBIT旧功能调用?
我试图在扑朔迷离的项目中使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦函数被调用,您就无法取消它,但是您可以做的是在 future 完成后放置一个检查器来检查函数是否需要继续前进。
我写了一个示例代码来帮助您更好地理解这一点,您可以粘贴此 dartpad 亲自尝试。
上面代码的输出如下:
这是一个相当简单的实现,可以帮助您理解如何基于外部因素中途退出函数的概念,您可以创建一个函数来为您完成此任务,而不是手动更改一个布尔值。
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.
The output of the above code is as follows:
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.
我找到了解决此问题的好方法,您可以使用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 .