gpt4 book ai didi

list - 尝试填写类(class)列表

转载 作者:行者123 更新时间:2023-12-03 03:34:19 26 4
gpt4 key购买 nike

我真的很抱歉,我是一个初学者。
最后,当应该是8个不同的名称/角色时,我只有8个具有相同名称/位置的TimeCardDayStrip列表。
我不明白自己在做什么错:
非常感谢你

'''
class TimeCardDayStrip {
String name, position;
DateTime day;

@override
String toString(){
return '{ $this.name, $this.position }';
}
}

void main() {
var tcds = TimeCardDayStrip();
var listOfTcds = [];
List<String> names, roles;
names = ["Michael", "Gunnell", "Byrne", "Aspromonte", "Davis", "Adam Jordan", "Mirko"];
roles = ["Director", "Vice", "President", "1ST", "KEY 2ND", "2ND", "BASECAMP PA", "PA", " PA", "Add'l PA"];

for (int i = 0; i < names.length; i++) {
tcds.name = names[i];
tcds.position = roles[i];
listOfTcds.add(tcds);
// checking the list as it creates
print(listOfTcds[i].name+' is '+listOfTcds[i].position);
}
//print the list of card to check
print('print the list of time card to check');
for (int x = 0; x < listOfTcds.length; x++){
print (listOfTcds.elementAt(x).name);
}
}
'''

最佳答案

如果您只想打印每个名称:
将其放入循环中(您应该在每个循环中创建新对象,而不是更新相同的对象)

 for (int i = 0; i < names.length; i++) {

var tcds = TimeCardDayStrip();

tcds.name = names[i];
tcds.position = roles[i];
listOfTcds.add(tcds);

// checking the list as it creates
print('${listOfTcds[i].name} is ${listOfTcds[i].position}');
}

然后再
 listOfTcds[x].name 

如果要显示内容:
  @override
Widget build(BuildContext ctx) {
return ListView.builder(
itemCount: listOfTcds.length,
itemBuilder: (context, index) {
return CustomTile(item: listOfTcds[index]);
},
);
}

关于list - 尝试填写类(class)列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61965298/

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