-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_screen.dart
226 lines (204 loc) · 5.31 KB
/
stream_screen.dart
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import 'dart:async';
import 'package:flutter/material.dart';
class StreamScreen extends StatefulWidget {
@override
_StreamScreenState createState() => _StreamScreenState();
}
class _StreamScreenState extends State<StreamScreen> {
final List<String> methods = [
"Periodic",
"Take",
"TakeWhile",
"Skip",
"SkipWhile",
"Where",
"ToList",
"ForEach",
"Length",
];
List<String> dataList = List.generate(9, (_) => "");
StreamSubscription _subscription;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Stream Demo'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.stop),
backgroundColor: Colors.red,
onPressed: _stopSubscription,
tooltip: "Stop Any Stream",
),
body: ListView.builder(
itemCount: methods.length,
itemBuilder: (context, index) {
return ListTile(
trailing: Text(
dataList[index],
style: TextStyle(color: Colors.grey, fontSize: 18),
),
title: Text(methods[index]),
onTap: () => _performTasks(index),
);
},
),
),
);
}
void _performTasks(int index) {
if (_subscription != null) {
_stopSubscription();
}
switch (index) {
case 0:
_performStreamPeriodic();
break;
case 1:
_performStreamTake();
break;
case 2:
_performStreamTakeWhile();
break;
case 3:
_performStreamSkip();
break;
case 4:
_performStreamSkipWhile();
break;
case 5:
_performStreamWhere();
break;
case 6:
_performStreamToList();
break;
case 7:
_performStreamForEach();
break;
case 8:
_performStreamLength();
break;
default:
break;
}
}
// * It will print 1, 2, 3 ... endlessly.
void _performStreamPeriodic() {
_subscription =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => (int + 1))
.listen((data) {
setState(() {
dataList[0] = "$data";
});
});
}
// * It will print 2, 4, 6, 8, 10 and stop.
void _performStreamTake() {
_subscription = Stream<int>.periodic(
Duration(milliseconds: 200), (int) => (int + 1) * 2)
.take(5)
.listen((data) {
setState(() {
dataList[1] = "$data";
});
});
}
// * It will only print even number from 1, 2, 3, ... until 10.
void _performStreamTakeWhile() {
_subscription =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.takeWhile((data) => data <= 10)
.listen((data) {
setState(() {
dataList[2] = "$data";
});
});
}
// * It will skip the first 2 numbers and take 5 numbers
// * So it will print 3, 4, 5, 6, 7
void _performStreamSkip() {
_subscription =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.skip(2)
.take(5)
.listen((data) {
setState(() {
dataList[3] = "$data";
});
});
}
// * It will skip until the number is bigger than 2
// * So it will print 3, 4, 5, ... endlessly.
void _performStreamSkipWhile() {
_subscription =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.skipWhile((data) => data < 2)
.listen((data) {
setState(() {
dataList[4] = "$data";
});
});
}
// * It will only print the prime numbers endlessly.
void _performStreamWhere() {
_subscription =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.where((data) => isPrime(data))
.listen((data) {
setState(() {
dataList[5] = "$data";
});
});
}
// * await some times and turn stream into general List.
void _performStreamToList() async {
var stream =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.take(5);
// * It should wait 200 * 5 milliseconds
List<int> data = await stream.toList();
setState(() {
dataList[6] = "${data.toString()}";
});
}
// * Use [for each] to print the stream
void _performStreamForEach() {
var stream =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.take(5);
stream.forEach((int data) {
setState(() {
dataList[7] = "$data";
});
});
}
// * We can get the stream length when your length is fixed.
void _performStreamLength() async {
var stream =
Stream<int>.periodic(Duration(milliseconds: 200), (int) => int + 1)
.take(10);
// * It should wait 200 * 10 milliseconds
var length = await stream.length;
setState(() {
dataList[8] = "$length";
});
}
@override
void dispose() {
_stopSubscription();
super.dispose();
}
void _stopSubscription() {
if (_subscription != null) {
_subscription.cancel();
_subscription = null;
}
}
bool isPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0 && i != number) return false;
}
return true;
}
}