在異步代碼實驗室中:參考:https ://dart.dev/codelabs/async-await
他們有以下片段:
Future<void> printOrderMessage() async {
print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
// Imagine that this function is more complex and slow.
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
void main() async {
countSeconds(4);
await printOrderMessage();
}
基于 printOrderMessage() 的邏輯,我做了一個類似的函式:
void main() async {
int? value;
value = await test();
print(value);
}
Future<int?> test() async{
print('Function has started');
Future.delayed(Duration(milliseconds: 2000), () {
return 4;
});
}
就我而言,它列印 Function has started null 。為什么不等待填充值
uj5u.com熱心網友回復:
Future<int?> test() async {
print('Function has started');
await Future.delayed(Duration(milliseconds: 2000), () {});
return 4;
}
Future<int?> test() async {
print('Function has started');
return Future.delayed(Duration(milliseconds: 2000), () {
return 4;
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522600.html
標籤:扑镖
