颤振计算方法 [英] Flutter- compute method
本文介绍了颤振计算方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 compute()
执行繁重的方法。
我曾这样尝试过。
在执行循环
之后,更新了 Text
小部件,但结果
几乎在按下按钮后立即返回0。
有人知道我缺少什么吗?
I'm trying to execute heavy method by compute()
.
I tried like so.
After loop
executed, Text
widget update but result
returns 0 almost right after button was pressed.
Does anyone know what am I missing?
int _counter;
static int loop(int val) {
int count = 0;
for (int i = 1; i <= val; i++) {
count += i;
}
return count;
}
Future<void> _onPressed() async {
int result = await compute(loop, 1000000000000000000);
setState(() {
_counter = result;
});
}
推荐答案
根据 https://docs.flutter.io/flutter/foundation/compute.html ,
回调参数必须是顶层函数,而不是类的闭包,实例或静态方法。
The callback argument must be a top-level function, not a closure or an instance or static method of a class.
请删除 static
关键字,它应该可以正常工作
Please remove the static
keyword, it should work fine
这篇关于颤振计算方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文