gpt4 book ai didi

dart - Flutter-计算方法

转载 作者:IT王子 更新时间:2023-10-29 06:37:17 25 4
gpt4 key购买 nike

我正在尝试通过 compute() 执行繁重的方法。
我这样试过。loop 执行后,Text 小部件更新,但 result 几乎在按下按钮后立即返回 0。
有谁知道我错过了什么?

 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://api.flutter.dev/flutter/foundation/compute-constant.html ,

The callback argument must be a top-level function, not a closure or an instance or static method of a class.

请删除 static 关键字,它应该可以正常工作

关于dart - Flutter-计算方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52196555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com