gpt4 book ai didi

flutter - flutter 列表返回实例

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

我从json获取数据,但是当尝试将数据添加到列表然后打印列表时,获取

class User {
final String name;

User(this.name);
}
....................
Map<String, dynamic> myMap = jsonData;
List<dynamic> lst = myMap["items"];
for(var u in lst ){
User user = User(u['name']);
print(u['name']); //return name eg. Marc
users.add(user);
}

print(users); //return Instance of
的实例

最佳答案

覆盖toString()类中的User

像这样

class User {
final String name;

User(this.name);

@override toString() => 'User: $name';
}

然后你可以使用
void main() {
var users = [User("a"), User("b"), User("c")];
print(users);

print('----------------');

for (var user in users) print(user);
}

关于flutter - flutter 列表返回实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039488/

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