簡體   English   中英

這個列表是如何使用地圖集成到下拉按鈕項目中的?誰能解釋一下?

[英]How is this list is integrated in Dropdownbutton items using map?Can anyone explain?

這是在 Class state 中定義的:

static var _priorities = ["High", "Low"]; 強文本

這是在構建小部件內:

ListTile( title: DropdownButton( items: _priorities.map((String dropDownStringItem) { return DropdownMenuItem<String>( value: dropDownStringItem, child: Text(dropDownStringItem), ); }).toList(), onChanged: null), )

來自List.map上的Dart 文檔

返回一個新的惰性 Iterable,其中包含通過按迭代順序在此 Iterable 的每個元素上調用 f 創建的元素。

也就是說, map返回一個Iterable ,其中包含原始List轉換的每個元素。 列表中項目的轉換方式取決於傳遞給map方法的閉包。 在你的情況下:

  1. 您從_priorities列表中獲取每個String :“高”和“低”。
  2. 通過將DropDownMenuItem Function(String)傳遞給map方法,您可以為它們中的每一個構建一個DropDownMenuItem<String> 您傳遞給map的閉包是一個函數,它接受一個String並返回一個DropDownMenuItem
  3. 然后,您調用toList()map返回的Iterable轉換為List

這樣一來,你已經改變了你List<String>一個List<DropDownMenuItem>通過采取每String原始列表,建設DropDownMenuItem每一個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM