想要推遲某些代碼行,使用 await Future 并且效果很好,問題是它推遲了它之后的所有代碼,我需要它推遲某些代碼行,同時繼續立即執行其余代碼
void main() async {
for (int i = 0; i < 5; i ) {
await Future.delayed(Duration(seconds: 1));
//postpone just next line or few lines of code
print('postpone this line of code ${i 1}');
print('postpone me too');
}
//should execute without being postponed
print('continue imediately without being postponed by await Future');
}
這可以通過 await Future 或其他一些功能實作嗎?
uj5u.com熱心網友回復:
await它是注冊Future.then回呼的語法糖。使用的目的await是更容易讓所有后續代碼等待Future完成。如果這不是你想要的,你可以Future.then直接使用:
void main() {
for (int i = 0; i < 5; i ) {
Future.delayed(Duration(seconds: 1)).then((_) {
print('postpone this line of code ${i 1}');
print('postpone me too');
});
}
print('continue immediately without being postponed by await Future');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367323.html
