我有一個items引數,我必須分配串列小部件,如下所示
items: [
CachedNetworkImage(
imageUrl: "https://images.unsplash.com/photo-1534531173927-aeb928d54385?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870",
height: double.infinity,
fit: BoxFit.cover,
),
CachedNetworkImage(
imageUrl: "https://images.unsplash.com/photo-1489824904134-891ab64532f1?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1031",
height: double.infinity,
fit: BoxFit.cover,
)
],
我正在通過動態串列嘗試同樣的事情,我的資料看起來像
List <Carousel> carousels = [
const Carousel(
imageUrl: "https://images.unsplash.com/photo-1534531173927-aeb928d54385?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870"
),
const Carousel(
imageUrl: "https://images.unsplash.com/photo-1489824904134-891ab64532f1?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1031"
),
const Carousel(
imageUrl: "https://images.unsplash.com/photo-1628964178609-aec11c666040?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=871"
),
const Carousel(
imageUrl: "https://images.unsplash.com/photo-1505705694340-019e1e335916?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=80&raw_url=true&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1032"
),
];
現在在專案中,我使用串列生成器來獲取專案中的串列小部件
items: [
ListView.builder(
itemCount: carousels.length,
itemBuilder: (BuildContext context, int index) {
return CachedNetworkImage(
imageUrl: carousels[index].imageUrl,
height: double.infinity,
fit: BoxFit.cover,
);
},
),
]
在這里我收到錯誤Another exception was thrown: Null check operator used on a null value,如何在輪播串列中的專案中創建小部件串列?
uj5u.com熱心網友回復:
試試這個示例代碼:
List<Widget> _listItem = [];
for(var imageWidget in carousels){
Widget newWidget = CachedNetworkImage(
imageUrl: imageWidget.imageUrl,
height: double.infinity,
fit: BoxFit.cover,
);
_listItem.add(newWidget);
}
items = _listItem;
uj5u.com熱心網友回復:
items = carousels.map( (Carousel c) =>
CachedNetworkImage(
imageUrl: c.imageUrl,
height: double.infinity,
fit: BoxFit.cover,
)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/479975.html
上一篇:沒有為“FirebaseAuth”型別定義方法“signInWithAnonymously”
下一篇:顫振異步功能不同步
