欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > Flutter Isolate解决耗时任务导致卡死

Flutter Isolate解决耗时任务导致卡死

2025/2/10 6:15:24 来源:https://blog.csdn.net/Wuxiaoming135/article/details/145504055  浏览:    关键词:Flutter Isolate解决耗时任务导致卡死

先来对比一下在Flutter的ui主线程下直接计算一个耗时任务的情况:

import 'package:flutter/material.dart';void main() {runApp(const MaterialApp(home: H(),));
}class H extends StatefulWidget {const H({super.key});@overrideState<H> createState() => _HState();
}class _HState extends State<H> {int _count = 0;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("耗时计算"),),body: Center(child: Text("$_count"),),floatingActionButton: FloatingActionButton(child: const Text("start"),onPressed: () {_count = countPrimes(100000000);setState(() {});}),);}
}// 计算质数个数
int countPrimes(int n) {List<bool> isPrime = List.filled(n + 1, true);isPrime[0] = isPrime[1] = false;for (int i = 2; i * i <= n; i++) {if (isPrime[i]) {for (int j = i * i; j <= n; j += i) {isPrime[j] = false;}}}return isPrime.where((prime) => prime).length;
}

发现点击按钮后,直接卡死,现在换成异步执行:

import 'package:flutter/material.dart';void main() {runApp(const MaterialApp(home: H(),));
}class H extends StatefulWidget {const H({super.key});@overrideState<H> createState() => _HState();
}class _HState extends State<H> {int _count = 0;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("耗时计算"),),body: Center(child: Text("$_count"),),floatingActionButton: FloatingActionButton(child: const Text("start"),onPressed: () async {_count = await countPrimes(100000000);setState(() {});}),);}
}// 计算质数个数
Future<int> countPrimes(int n) async {List<bool> isPrime = List.filled(n + 1, true);isPrime[0] = isPrime[1] = false;for (int i = 2; i * i <= n; i++) {if (isPrime[i]) {for (int j = i * i; j <= n; j += i) {isPrime[j] = false;}}}return isPrime.where((prime) => prime).length;
}

发现仍旧会卡死,因为计算过程还是发生在ui线程中,现在使用isolate进行对比:

import 'dart:isolate';import 'package:flutter/material.dart';void main() {runApp(const MaterialApp(home: H(),));
}class H extends StatefulWidget {const H({super.key});@overrideState<H> createState() => _HState();
}class _HState extends State<H> {int _count = 0;ReceivePort? _receivePort;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("耗时计算"),),body: Center(child: Text("$_count"),),floatingActionButton: FloatingActionButton(child: const Text("start"),onPressed: () {_receivePort = ReceivePort();Isolate.spawn(countPrimes, [100000000, _receivePort!.sendPort]);_receivePort!.listen((message) {setState(() {_count = message;});});}),);}
}// 计算质数个数
void countPrimes(List<dynamic> args) {int n = args[0];SendPort sendPort = args[1];List<bool> isPrime = List.filled(n + 1, true);isPrime[0] = isPrime[1] = false;for (int i = 2; i * i <= n; i++) {if (isPrime[i]) {for (int j = i * i; j <= n; j += i) {isPrime[j] = false;}}}sendPort.send(isPrime.where((prime) => prime).length);
}

发现问题得到解决

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com