У меня есть тестовый код ниже:
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 раз, и порядок всегда был одинаковым с каждой сборкой. Из-за асинхронного программирования c мне интересно, "Всегда ли этот порядок сохраняется при каждом запуске?"