Isolate的使用
1.Isolate的简介
Isolate是Dart语言中处理并发的基本单元,每个Isolate都有自己的内存堆和消息通信通道,可以独立地执行任务而不受其他Isolate的影响。
Flutter中,可以使用Isolate来执行耗时的计算、IO操作或其他需要异步处理的任务,以确保不阻塞应用的主线程。
2.示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import 'package:flutter/material.dart';
import 'dart:isolate';
class IsolateWidget extends StatefulWidget {
const IsolateWidget({super.key});
@override
IsolateWidgetState createState() => IsolateWidgetState();
}
class IsolateWidgetState extends State<IsolateWidget> {
late ReceivePort _receivePort;
String _message = '';
int _counter = 0;
@override
void initState() {
super.initState();
initIsolate();
}
@override
void dispose() {
super.dispose();
_receivePort.close();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('message: $_message'),
const SizedBox(
height: 20,
),
ElevatedButton(onPressed: () {
setState(() {
_counter++;
});
_receivePort.sendPort.send('Hello from main:$_counter');
}, child: const Text('Isolate使用')),
],
);
}
void initIsolate() async {
// 创建接收端口
_receivePort = ReceivePort();
// 将接收端口的发送端口发送到Isolate
// 监听来自Isolate的消息
SendPort sendPort = _receivePort.sendPort;
Isolate isolate = await Isolate.spawn(entryPoint, sendPort);
_receivePort.listen((message) {
debugPrint('Message from isolate: $message');
if (message is String) {
setState(() {
// 更新UI
_message = message;
});
}
});
}
}
void entryPoint(SendPort sendPort) {
// 在这里执行耗时任务
String result = 'Result of time-consuming task';
sendPort.send(result);
}
|
3.效果
1
2
3
4
5
6
7
8
|
flutter: Message from isolate: Result of time-consuming task
flutter: Message from isolate: Hello from main:1
flutter: Message from isolate: Hello from main:2
flutter: Message from isolate: Hello from main:3
flutter: Message from isolate: Hello from main:4
flutter: Message from isolate: Hello from main:5
flutter: Message from isolate: Hello from main:6
flutter: Message from isolate: Hello from main:7
|
4.小结
文章作者
梵梵爸
上次更新
2024-03-21
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢