【发布时间】:2020-07-21 04:06:44
【问题描述】:
示例代码(无效):
void main() {
List json = [
{
"sku": "SKU0001",
"uids": [
{
"uid": "U001"
}
]
}
];
var result = json.map( (item) {
item['no_desc'] = true; // able to add the key and bool value
item['uids'] = item['uids'].map( (uid) {
uid['is_scanned'] = true; // failed to add, but able to accept string value only.
return uid;
}).toList();
return item;
}).toList();
print(result);
}
返回错误'bool'不是'String'类型的子类型
预期结果
[
{
sku: SKU0001,
no_desc: true,
uids: [
{
uid: U001,
is_scanned: true // must be boolean
}
]
}
]
当我尝试String 值时它会起作用。
uid['is_scanned'] = 'true';
如何将布尔值添加到嵌套列表中?
如果我用 javascript 方式编写, 它应该能够将键添加到嵌套数组(列表)中。
但是为什么在 dart lang 中,它提示我错误? 有哪位飞镖专家愿意给我解释一下吗?
【问题讨论】: