如何在 Dart 的 Map 中添加动态值?
How to add dynamic value in Map in Dart?
我有一个动态 UI,可能是下拉菜单或文本字段!
我需要准备地图发送到后端。下面是我需要从动态 UI,
准备的地图格式
Map temp;
temp = {
"receiver": {
"Bank": {"accounnt": '123'},
}
};
if(temp['receiver']['school'] == null){
temp['receiver']['school'] = '12';
}
但是当我尝试添加 temp['receiver']['school'] = '12';
时出现错误。错误指出,type 'String' is not a subtype of type 'Map<String, String>' of 'value.'
我想创建以下格式的地图,
"receiver": {
"bank_account": {
"swift_bic": "XXX",
"account_number": "225225"
},
"reason_for_sending": "Family"
}
Error image: https://i.stack.imgur.com/5yg2t.png
试试这个:
void main() {
Map<String,Map<String,dynamic>> temp;
temp = {
"receiver": {
"Bank": {"accounnt": '123'}
}
};
if(temp['receiver']!['school'] == null){
temp['receiver']!['school'] = '12';
}
print(temp);
}
我有一个动态 UI,可能是下拉菜单或文本字段!
我需要准备地图发送到后端。下面是我需要从动态 UI,
准备的地图格式Map temp;
temp = {
"receiver": {
"Bank": {"accounnt": '123'},
}
};
if(temp['receiver']['school'] == null){
temp['receiver']['school'] = '12';
}
但是当我尝试添加 temp['receiver']['school'] = '12';
时出现错误。错误指出,type 'String' is not a subtype of type 'Map<String, String>' of 'value.'
我想创建以下格式的地图,
"receiver": {
"bank_account": {
"swift_bic": "XXX",
"account_number": "225225"
},
"reason_for_sending": "Family"
}
Error image: https://i.stack.imgur.com/5yg2t.png
试试这个:
void main() {
Map<String,Map<String,dynamic>> temp;
temp = {
"receiver": {
"Bank": {"accounnt": '123'}
}
};
if(temp['receiver']!['school'] == null){
temp['receiver']!['school'] = '12';
}
print(temp);
}