【发布时间】:2021-03-09 18:10:20
【问题描述】:
我正在阅读async-await 文档并尝试使用它来使函数等待返回。根据我的理解(我是异步的新手),async 函数在第一个 await 关键字之前逐行执行,但在下面的代码中我不能这样做。我怎样才能做到这一点?
List<Map> testinglist = [];
//function that obtain data somwhere and where I want to work on
Future<void> _getEventData() async {
testinglist.clear();
debugPrint('right after clear');
print(testinglist.length);
//await here to get the return from database
var snapshot2 = await fireBaseDB.child('event').once();
Map map2 = snapshot2.value;
//Problem here: I used await here, so I supposed line after this execute only after this is finished?
await map2.keys.toList().forEach((element) {
fireBaseDB
.child('event')
.child(element)
.once()
.then((DataSnapshot innersnapshot) {
testinglist.add(innersnapshot.value);
debugPrint('after entering');
print(testinglist.length);
});
});
debugPrint('end entering');
print(testinglist.length);
debugPrint('end');
}
@override
Widget build(BuildContext context) {
...
new RaisedButton(
child: new Text("print data"),
onPressed: () {
debugPrint('before entering');
print(testinglist.length);
_getEventData();
},
new RaisedButton(
child: new Text("check length"),
onPressed: _checkingLength,
color: Colors.redAccent,
),
...
}
The output isn't as I expected:
I/flutter (11527): before entering
I/flutter (11527): 3
I/flutter (11527): right after clear
I/flutter (11527): 0
I/flutter (11527): end entering
I/flutter (11527): 0
I/flutter (11527): end
I/flutter (11527): after entering
I/flutter (11527): 1
I/flutter (11527): after entering
I/flutter (11527): 2
I/flutter (11527): after entering
I/flutter (11527): 3
为什么after entering 不在end entering 之前?我理解 async 和 await 错误吗?我该如何解决这个问题
【问题讨论】:
标签: flutter asynchronous async-await