【发布时间】:2020-01-08 05:00:55
【问题描述】:
我有一个测试代码如下:
class SplashPageState extends State<SplashPage> {
Future<void> dummy() async {
print('Async function1');
Future.delayed(Duration.zero, () => print('Async function2'));
print('Async function3');
await Future.delayed(Duration.zero, () => print('Async function4'));
print('Async function5');
}
@override
void initState() {
print('initState 1');
super.initState();
print('initState 2');
Future.sync(() {
print('Future sync');
});
print('initState 3');
Future.delayed(Duration.zero, () {
print('Future value');
});
print('initState 4');
dummy();
print('initState 5');
}
@override
Widget build(BuildContext context) {
print('build');
return Scaffold(
appBar: AppBar(
title: Text('1'),
),
body: Text('2'),
);
}
}
结果:
I/flutter (16218): initState 1
I/flutter (16218): initState 2
I/flutter (16218): Future sync
I/flutter (16218): initState 3
I/flutter (16218): initState 4
I/flutter (16218): Async function1
I/flutter (16218): Async function3
I/flutter (16218): initState 5
I/flutter (16218): build
I/flutter (16218): Future value
I/flutter (16218): Async function2
I/flutter (16218): Async function4
I/flutter (16218): Async function5
我测试了 100 次,每次构建的顺序始终相同。由于异步编程,我想知道“这个顺序是否总是保持每次运行?”
【问题讨论】:
标签: asynchronous flutter dart async-await