我創建了一個串列“數字”,然后使用“地圖”方法對其進行回圈并將輸出分配給正方形。現在我在“squares”上應用 toList() 方法將其轉換為 List 型別,但在輸出中它被列印為 int 的可迭代。但是,當我在 print() 方法中執行相同的操作時,它會將輸出作為串列提供給我。那么為什么它不能在 print() 方法之外作業呢?
void main() {
const numbers = [1, 2, 3, 4];
final squares = numbers.map((number) => number * number);
squares.toList(); // I've applied toList() method here
print(squares); // But I don't get a list from this output
print(squares.toList()); // But I get it from this output. Why?
}
輸出 (1, 4, 9, 16) [1, 4, 9, 16]
uj5u.com熱心網友回復:
toList()將資料轉換為串列,但您必須將結果分配給如下變數:
List list = squares.toList();
然后您可以使用新變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/489959.html
