簡體   English   中英

如何將 class 添加到 dart 中?

[英]How to add class to list in dart?

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一起使用,但除非你在列表中使用class,否則沒有數組嗎?

as your question is not clear what you want to ask but what I understand is you want to add class object in list.... For this you have can specify list type then add object of class to it. 喜歡,

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);
  }
}

我希望我能正確理解你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM