[Dart]for in, Enum, Funtion

1578 ワード

for in

void main() {
  List numbers = [
    1,
    2, 
    3, 
    4, 
    5, 
    6,
  ];
  
  int total = 0;
  
  for(int number in numbers) {
    total += number;
  }
  print(total); 
  
  // 단점: 현재 index를 알 수가 없다
} 

Enum


条件文はより安全に使用できます.
enum Type {
   approved,
   rejected,
   pending
}
Type type = Type.approved;

if(type == Type.approved){
print("승인");
}

Print(Type.values.toList()); //type value 전부 출력 [Status.approved, Status.rejected, Status.pending]

Funtion

 List testList = [1, 2, 3, 4, 5];

int addList(List list) {
  int total = 0;

  for (int number in list) {
    total += number;
  }

  return total;
}
int result = addList(testList); // 15  변수에 담을 수 있음

Funtion parameter

 List testList = [1, 1, 2, 3, 4, 5, 8];
 List testList2 = [2, 3, 4, 5, 1, 2];

 int result = addList(testList2, 1);
 int result2 = addList(testList, 1, d: 2, b: 1, c: 10); 

// []안의 값은 안 넣어도 됨,
// 순서 지켜야함
int addList(List list, int a, [int b = 3]) { 
  print('b 값은: $b');
  int total = 0; 

  for (int number in list) {
    total += number;
  }

  return total;
}

//순서에 구애받지 않음
int addList2(List list, int a, {int b, int c, int d, int e}) {
  int total = 0;
 print('b: $b, c: $c, d: $d, e: $e');
  // b: 1, c: 10, d: 2, e: null
  // b: null, c: null, d: null, e: null

  for (int number in list) {
    total += number;
  }

  return total;
}