【发布时间】:2020-05-23 20:54:59
【问题描述】:
我有一个字典列表,其中有一个 ID。 我遍历列表并创建一个凸起的按钮。 提升按钮“onPressed”旨在发送创建时“产品”中的“id”,但它始终发送列表中最后一个“产品”对象的“id”。它似乎保留了与产品对象的相对链接,而不是“id”的绝对值。 (**仅用于突出显示对象,不在代码中)。
if(productList!=null)
for(product in productList)
TableRow(
children: <Widget>[
Container(
decoration: new BoxDecoration(
color: Colors.white,
),
child: Center(
child: Row(
children: <Widget>[
SizedBox(
width: 50,
height: 20,
child:RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
),
onPressed: () => editProduct('**${product['id']}**'),
color: Colors.blue,
padding: EdgeInsets.fromLTRB(1, 1, 1, 1),
child: Text('Edit',
style: TextStyle(
color: Colors.white,
fontSize: 10,
),
),
),
),
],
),
],
),
),
),
],
),
我已经尝试了所有我能想到的转换为字符串的组合。 toString(),用单引号、双引号括起来。有什么建议吗?
【问题讨论】:
-
您能说明该项目的使用地点吗?如果在 ListView 或类似上,您可以将索引值与列表的长度进行比较,然后从该列表中获取值。请提供额外的信息,我将为您创建一个示例。
-
从代码中可以看出,我使用的是一个表格,而有问题的对象(用 ** ** 突出显示)是 SizedBox 内的 RaisedButton 的“onPressed”操作TableRow 内的容器内的中心内的行。在 ListView 中使用时,系统会保留完成时使用的索引,以便所有 onPressed 对象的索引值都是列表中的对象数,并且您会收到“超出范围”错误。
标签: flutter flutter-web