【问题标题】:How to add class to list in dart?如何在飞镖列表中添加类?
【发布时间】:2022-01-24 07:32:51
【问题描述】:
class CCC {
  int num;

  CCC(this.num) : this.num = num;
}

void main() {
  List list = [];
  CCC c1 = CCC(10);
  CCC c2 = CCC(20);

  print(list);
}

我真的不知道。需要二维数组配合db,但除非你使用列表中的类,否则不存在数组吗?

【问题讨论】:

  • 您能否澄清您在构建 x 结果时遇到的问题,请在发布前访问here

标签: flutter dart


【解决方案1】:

因为您的问题不清楚您想问什么,但我的理解是您想在列表中添加类对象....为此,您可以指定列表类型,然后向其中添加类对象。喜欢,

class CCC{
  final int num;
  CCC(this.num);
}
void main(){
  // specify the list type
  List<CCC> list = [];
  CCC c1 = CCC(10);
  CCC c2 = CCC(20);
  list.add(c1);
  list.add(c2);
  // here you can print all items' property
  for (var item in list){
    print(item.num);
  }
}

希望我理解你的问题

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2018-10-15
    • 2021-10-12
    • 2021-12-11
    相关资源
    最近更新 更多