Skip to content

Commit c633265

Browse files
committed
feature: 增加异步相关
1 parent ebd6f0a commit c633265

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

dart_language/async.md

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 异步
22

3-
同步写法
3+
同步写法
44

55
```dart
66
// This example uses the Google Books API to search
@@ -19,30 +19,8 @@ http.get(url).then((result)=>print(result.body));
1919

2020
修改自[DartPad提供的样例](https://dartpad.cn/?id=4a68e553746602d851ab3da6aeafc3dd)
2121

22-
## Flutter
22+
## Future vs Stream
2323

24-
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
24+
单次 vs 多次
2525

26-
```dart
27-
Widget buildOutput(input){
28-
return FutureBuilder(
29-
// https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
30-
future: executor.execute(input),
31-
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
32-
if (snapshot.connectionState == ConnectionState.done) {
33-
// 请求已结束
34-
if (snapshot.hasError) {
35-
// 请求失败,显示错误
36-
return Text("Error: ${snapshot.error}");
37-
} else {
38-
// 请求成功,显示数据
39-
return Text("${snapshot.data}");
40-
}
41-
} else {
42-
// 请求未结束,显示正在加载的状态
43-
return const Text('代码运行中...');
44-
}
45-
},
46-
);
47-
}
48-
```
26+
详见:https://www.digitalocean.com/community/tutorials/flutter-futures-and-streams-dart

widgets/asnyc.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 异步组件
2+
3+
## FutureBuilder
4+
5+
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
6+
7+
```dart
8+
Widget buildOutput(input){
9+
return FutureBuilder(
10+
// https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
11+
future: executor.execute(input),
12+
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
13+
if (snapshot.connectionState == ConnectionState.done) {
14+
// 请求已结束
15+
if (snapshot.hasError) {
16+
// 请求失败,显示错误
17+
return Text("Error: ${snapshot.error}");
18+
} else {
19+
// 请求成功,显示数据
20+
return Text("${snapshot.data}");
21+
}
22+
} else {
23+
// 请求未结束,显示正在加载的状态
24+
return const Text('代码运行中...');
25+
}
26+
},
27+
);
28+
}
29+
```

0 commit comments

Comments
 (0)