void main() {
//异步执行的代码
print('say Hello');
//异步执行
Future.delayed(new Duration(seconds: 3),(){
print('chibaole');
});
print('play game');
}
执行顺序是:
1.say Hello
2.play game
3.chibaole // 等待3秒
- 异步执行做成同步的效果 需要使用async和await关键字 【串行】
void main() async{
print('say Hello');
//异步执行
await Future.delayed(new Duration(seconds: 3),(){
print('chibaole');
});
print('play game');
}
执行顺序是:
1.say Hello
2.chibaole // 等待3秒
3.play game
void main() async{
Future.wait([
Future.delayed(new Duration(seconds: 1),(){
print('001');
}),
Future.delayed(new Duration(seconds: 3),(){
print('002');
}),
Future.delayed(new Duration(seconds: 2),(){
print('003');
}),
]).then((List results){//then 是所有都执行完之后走的回调 results是上面三个异步的结果拼到results里面来
print('all over');
});
}
执行顺序是:
1: 001
2: 003
3: 002
4: all over
QQ:522608370
点个赞呗😊!!!
网友评论