gpt4 book ai didi

dart - 将Javascript对象重写为Dart映射/列表

转载 作者:行者123 更新时间:2023-12-03 03:11:05 25 4
gpt4 key购买 nike

我正在尝试将以下javascript对象转换为Dart映射:

var users = { 
1 : {
first_name: 'James',
last_name: 'Smith',
email: 'jsmith@example.com',
},
2 : {
first_name: 'Robin',
last_name: 'Doe',
email: 'rdoe@example.com',
}

我试过了:

var users = { 
"1" : {
first_name: 'James',
last_name: 'Smith',
email: 'jsmith@example.com',
},
"2" : {
first_name: 'Robin',
last_name: 'Doe',
email: 'rdoe@example.com',
}

但我无法将其用作带引号或不带引号的 map (引发错误)。

var keys = users.getKeys(); //NoSuchMethodError : method not found: 'getKeys'
assert(keys.length == 2);
assert(new Set.from(keys).contains('2'));

最佳答案

属性名称使用(单引号或双引号):

var users = {
"1" : {
"first_name": "James",
"last_name": "Smith",
"email": "jsmith@example.com",
},
"2" : {
"first_name": "Robin",
"last_name": "Doe",
"email": "rdoe@example.com"
}
};

另外, getKeys方法不存在,请改用 keys:

 var keys = users.keys;
assert(keys.length == 2);
assert(keys.contains("2"));
assert(users["1"]["first_name"] == "James");

关于dart - 将Javascript对象重写为Dart映射/列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13904170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com