【问题标题】:Is there a way to solve the problem of index inside the map?有没有办法解决地图内的索引问题?
【发布时间】:2022-01-21 00:52:33
【问题描述】:

我从DUMMY_MEALS 获取数据并尝试使用map 提取它,当我提取它时它工作正常但是当我想使用Dismissible 时我需要Index 而我不需要知道如何获得它。


我想解决这个问题,以便我可以删除该项目


注意:我尝试使用 ListView.builder,但没有成功

Column(
              children: [
                SizedBox(height: 15,) ,
                Text('ingredients' , style: TextStyle(fontSize: 25 , fontWeight: FontWeight.bold),),
                Divider(color: Colors.pink , thickness: 1,),
                ...selectMeal.ingredients
                      .map((e) => Dismissible(
                   onDismissed: (DismissDirection Dir){
                     setState(() {
                       selectMeal.ingredients.removeAt(index);
                     });
                   },
                          key: ObjectKey(e),
                          child: Card(
                            child: Padding(
                              padding: const EdgeInsets.all(15.0),
                              child: Text(e , style: TextStyle(fontSize: 20),),
                            ),
                          ),
                        ))
                    .toList(),
              ],
            ),

【问题讨论】:

  • 你试过ListViewshrinkWrap:true 属性吗?

标签: flutter dart


【解决方案1】:

您可以使用for 循环

例如:

var list = [1,2,3];

Column(
  children: [
    Text('Title'),
    for(var i = 0; i<list.length; i++)
    Text('i: ${list[i]}')
  ],
)

或者在Iterable 上创建一个扩展方法,就像这里建议的那样:https://stackoverflow.com/a/60502389/6704033

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 2022-09-27
    • 2021-02-04
    • 1970-01-01
    • 2021-01-09
    • 2020-01-18
    • 1970-01-01
    • 2023-01-26
    相关资源
    最近更新 更多