flutter - 通过单击按钮在 Flutter 中停止 while 循环
问题描述
如何通过按下按钮来停止 while 循环。
while (thirdDigit <= 9 && stopLoop == false) {
while (sixDigits <= 999999 && stopLoop == false) {
perspective = prefix + thirdDigit.toString() + sixDigits.toString();
url = perspective + urlSuffix;
validateUrl(url, numberListTiles, perspective);
print('BotStore: $url');
sixDigits++;
}
thirdDigit++;
}
}
我希望在单击按钮并分配 stopLoop = true 时停止此循环。
但是无论我按下停止按钮多少次,循环都在主线程中运行,并且按钮不会停止循环。
如何在后台运行循环并且仍然有一个正常运行的应用程序?
解决方案
要在后台运行循环,请将其放入异步函数中:
Future<void> loop() async {
// Loop here
}
要在点击按钮时停止它,请在 setState() 中将 stopLoop 设置为 true:
onPressed: () => setState(() => stopLoop = true);
推荐阅读
- debugging - 启动 chrome 调试后,VSC 中设置的断点发生变化
- amazon-web-services - AppSync $util.autoId() 和 DynamoDB 分区和排序键设计问题
- haskell - 未解决的重载 - 尝试为自己的类创建 Int 实例
- visualization - 如何向此 Vega 可视化添加图例
- javascript - 使用花括号的 Javascript 属性声明
- html - 我的一个 div 被幻象顶部填充所困扰,似乎忽略了我的 css 文件
- python - numpy 数组上的 lambda 函数。这段代码有什么问题?
- mysql - Mysql 查询分组和限制 1 Desc
- carousel - 如何调整引导轮播以调整图像大小并使图像可链接?
- typescript - String.fromCharCode vs '0x' 的 TypeScript 错误