0%

Flutter 中的多线程

Flutter 中的多线程

在java中,使用Thread 就可以创建线程了,然后,可以通过共享数据。进行数据交换,使用锁进行线程之间的同步。在Flutter中,则是使用 Isolate 来创建线程的,下面是创建线程并加载数据的代码:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import 'dart:convert';
import 'dart:isolate';

void main() {
runApp(new SampleApp());
}

class SampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Sample App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new SampleAppPage(),
);
}
}

class SampleAppPage extends StatefulWidget {
SampleAppPage({Key key}) : super(key: key);

@override
_SampleAppPageState createState() => new _SampleAppPageState();
}

class _SampleAppPageState extends State<SampleAppPage> {
List widgets = [];

@override
void initState() {
super.initState();

loadData();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Sample App"),
),
body: new ListView.builder(
itemCount: widgets.length,
itemBuilder: (BuildContext context, int position) {
return getRow(position);
}));
}

Widget getRow(int i) {
return new Padding(
padding: new EdgeInsets.all(10.0),
child: new Text("Row ${widgets[i]["title"]}"));
}

loadData() async {
debugPrint("1");
ReceivePort receivePort = new ReceivePort();
debugPrint("2");
await Isolate.spawn(dataLoader, receivePort.sendPort);
debugPrint("3");
// The 'echo' isolate sends it's SendPort as the first message
SendPort sendPort = await receivePort.first;
debugPrint("4");

List msg = await sendReceive(
sendPort, "https://jsonplaceholder.typicode.com/posts");

debugPrint("5");
setState(() {
debugPrint("6");
widgets = msg;
});
}

// the entry point for the isolate
static dataLoader(SendPort sendPort) async {
debugPrint("7");
// Open the ReceivePort for incoming messages.
ReceivePort port = new ReceivePort();
debugPrint("8");
// Notify any other isolates what port this isolate listens to.
sendPort.send(port.sendPort);
debugPrint("9");

await for (var msg in port) {
debugPrint("10");

String data = msg[0];
SendPort replyTo = msg[1];

String dataURL = data;
debugPrint("14");
http.Response response = await http.get(dataURL);
debugPrint("15");
// Lots of JSON to parse
replyTo.send(json.decode(response.body));
debugPrint("16");
}
}

Future sendReceive(SendPort port, msg) {
debugPrint("11");
ReceivePort response = new ReceivePort();
debugPrint("12");
port.send([msg, response.sendPort]);
debugPrint("13");
return response.first;
}
}

在控制台中,它的输出是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
I/flutter ( 2549): 1
I/flutter ( 2549): 2
I/flutter ( 2549): 7
I/flutter ( 2549): 8
I/flutter ( 2549): 9
I/flutter ( 2549): 3
I/flutter ( 2549): 4
I/flutter ( 2549): 11
I/flutter ( 2549): 12
I/flutter ( 2549): 13
I/flutter ( 2549): 10
I/flutter ( 2549): 14
I/flutter ( 2549): 15
I/flutter ( 2549): 16
I/flutter ( 2549): 5
I/flutter ( 2549): 6

通过输出,可以看到代码执行的步骤