我有一個串列視圖及其從 api 服務中獲取的內容。我需要在 api 回應到來之前顯示一個微光,如果有資料形式的 api,則顯示 listview,如果沒有資料,則顯示一個空狀態訊息。請檢查我下面的代碼,我已經實作了。微光和串列項視圖作業正常,但如果串列為空,我的空狀態視圖不會顯示.. 它顯示為空白視圖。
productListWidget() {
return Expanded(
child: ListView.builder(
itemCount: isLoading? 5 : searchedProductList.length,
padding: EdgeInsets.only(left: 8, right: 8, top: 8, bottom: 8),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
if(isLoading){
return productItemShimmer();
}else {
print(isEmptyList);
return isEmptyList? pItemEmpty() :productItem(searchedUnitList[index], index) ;
}
},
));
uj5u.com熱心網友回復:
searchedProductList.length0當串列為空時回傳。你可以做
Expanded(
child: ListView.builder(
itemCount: isLoading
? 5
: searchedProductList.isEmpty
? 1
: searchedProductList.length,
itemBuilder: (context, index) {
if (isLoading) {
return Text("shimmer widget hereF");
} else {
print(searchedProductList.length);
return searchedProductList.isEmpty
? Text("Empty")
: Text("product Item $index");
}
},
),
當串列為空時,這將回傳單個小部件。
uj5u.com熱心網友回復:
如果您的串列為空,則ListView構建器將不會構建任何子級。您應該將空測驗移出串列生成器:
return Expanded(
child: isEmptyList ? pItemEmpty() : ListView.builder(
itemCount: isLoading? 5 : searchedProductList.length,
padding: EdgeInsets.only(left: 8, right: 8, top: 8, bottom: 8),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
if(isLoading){
return productItemShimmer();
}else {
return productItem(searchedUnitList[index], index) ;
}
},
));
此外,要根據操作狀態顯示不同的小部件,我建議使用FutureBuilder:https ://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454292.html
上一篇:java.lang.ClassCastException:android.text.SpannableStringBuilder不能轉換為com.ree.kms.DakaHinbanClass
