我的問題可能看起來很糟糕,但我只是找不到一個更清晰的方式來表達它。 情況是這樣的:
- 我有一個資訊串列,包含在一個叫做 "carnetList "的串列中(見以下代碼)。
- 我應該只顯示引數 "thematic "為false的資訊。 這作業很好。然而,我需要對我所顯示的資訊進行編號。我使用了 "index "來顯示 "List n°1 etc..."。但由于它跳過了一些元素(thematic == true),然后我得到了 "List n°1... then List n°4 etc..."。
是否有辦法添加另一個 "計數器"? 我可以用它來代替 "索引",它將實際對應于顯示的元素數量?
下面是代碼 :
ListView.builder(
cacheExtent。2000,
padding: const EdgeInsets.all(20)。
itemCount: uD.userInfo.carnetList.length。
itemBuilder: (context, index) {
return uD.userInfo.carnetList[index].thematic == false ?
? CarnetListCard2(
index: index, **//這是我用Index在widget中顯示 "List n $index "的地方。 **
taille: uD.userInfo.carnetList[index].carnetWordId.length。
titre: uD.userInfo.carnetList[index].titre。
dateCreation: uD.userInfo.carnetList[index].creation。
dateModification:
uD.userInfo.carnetList[index].modification。
test: uD.userInfo.carnetList[index].test。
dateTest:uD.userInfo.carnetList[index].testDate,)
: 容器()。
},
),
uj5u.com熱心網友回復:
你很接近了,但是你不希望在構建專案時過濾串列。如果你確保你的串列在傳遞給listview之前已經被過濾了,你會得到你想要的結果。最簡單的方法是:
final list = D.userInfo.carnetList.where((carnet) => !carnet.thematic).toList()。
return ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
final carnet = list[index];
final number = index 1;
...
}
)
或者如果你不想用一個單獨的串列來作業
ListView(
兒童。D.userInfo.carnetList.where((carnet) => !carnet.thematic).map(( item) {
return ... //你的小工具。
}).toList()。
);
uj5u.com熱心網友回復:
你也可以使用一個計數器,只要條件為真,你就增加它。
它將作為真正對我有利的專案的索引。
int _itemsCounter = 0;
ListView.builter(
cacheExtent: 2000,
padding: const EdgeInsets.all(20)。
itemCount: uD.userInfo.carnetList.length。
itemBuilder: (context, index) {
if (uD.userInfo.carnetList[index].thematic == false) _itemsCounter ;
return uD.userInfo.carnetList[index].thematic == false ?
? CarnetListCard2(
index: index, **//這是我用Index在widget中顯示 "List n $index "的地方。 **
taille: uD.userInfo.carnetList[index].carnetWordId.length。
titre: uD.userInfo.carnetList[index].titre。
dateCreation: uD.userInfo.carnetList[index].creation。
dateModification:
uD.userInfo.carnetList[index].modification。
test: uD.userInfo.carnetList[index].test。
dateTest:uD.userInfo.carnetList[index].testDate,)
: 容器()。
},
),
只要確保當你使用_itemsCounter作為索引時,要減去其中的1。
因此,如果3個專案滿足條件,這個計數器將是3,但元素的索引是0、1、2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/315717.html
標籤:
上一篇:顯示四個textField
