使用 carousel_slider 1.0.0我需要提供一个 map 来定义 slider 中的项目数量。
我能够使用“articlesMap”找到来自 API 的项目数量,但我需要将此整数转换为 List<Map>
因为 slider 使用以下输入:
items: [1,2,3,4,5].map((i) {})
我将如何转换它? slider 接收的项目数量不是静态的,因此我不能依赖输入 [1,2,3,4,5]
我可能会收到 +/- 5 件商品。
最佳答案
Dart 提供了一个列表生成器,您可以将其用作:
items: List<int>.generate(articlesList.length, (i) => i + 1).map...
只映射您的文章列表可能会更简单(您不显示您从哪里获得文章):
CarouselSlider(
height: 400.0,
items: articlesList
.map((a) => Builder(
builder: (BuildContext context) => Container(
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5.0),
decoration: BoxDecoration(color: Colors.amber),
child: Text(
a.name,
style: TextStyle(fontSize: 16.0),
),
),
))
.toList(),
);
如果文章真的在 map 中,您可以使用 .values
转换为列表
关于flutter - Dart - 在 map 中使用整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56843350/