无法访问动态生成的字符串列表



我动态生成了一个字符串列表。/

/I declared a list of strings and generated it using the length of my product.
late final List<String> recovered; 
//the length of the products is 3
recovered = List.generate(products.length, (index) => ""));

//我将生成的列表赋值给TextField中的onChanged方法。

TextField(
onChanged: (value) {
recovered[index] =
value;
log("the value is $value");
setState(() {});
},

}),

我想让每个字符串生成并分配给一个动态的地图。是这样的:{"product": "Noodles"; "recovered"; "2"}//recovered是从文本字段中获取的字符串,产品是从产品列表中获取的。我不能使用recover [index],因为它只返回第一个索引的字符串。我不能这样做恢复[1],因为字符串是动态生成的,我不能得到每个索引。

我找到了一个可行的解决方案。

List<Map> mapList = []; //Create a list of map
var myproduct = {}; //a map that will take the product and the recovered.

然后将myproduct赋值给TextField内的map (mapList)列表。

TextField(
onChanged: (value) {
recovered[index] =
value;
myproduct = {
"product": products[index]['product'],                                                                                                                "quantity_delivered": value
};                                                                mapList.add(myproduct);
}),

最新更新