我試圖在我的 dart 函式中使用可選引數默認值,但它導致錯誤 Error: Not a constant expression.
我試圖通過將我的變數插入一個回傳 的 lambda 函式來解決int,但它沒有用。看我的代碼
Stream<int> testAsync(int n, {int delay = 100}) async* {
if (n > 0) {
await Future.delayed(const Duration(microseconds: delay));
yield n;
yield* testAsync( n-1 );
}
}
有沒有辦法在 Delay 中使用可選的 int?
uj5u.com熱心網友回復:
const在Duration建構式上洗掉。您使用可選引數沒有問題。
Dart 無法從潛在可變函式引數創建編譯時常量。
Stream<int> testAsync(int n, {int delay = 100}) async* {
if (n > 0) {
await Future.delayed(Duration(microseconds: delay));
yield n;
yield* testAsync( n-1 );
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/318777.html
