본문 바로가기

Flutter/Dart

Dart- 데이터 타입 정리

List

list의 선언은 일반적인 다른 언어들과 동일하다. numbers는 List<int> 형으로 선언되었고 내부에는 collection if 문법이 사용되었다.

collection if

List<int> intList = [
  if(true) 1,
]

collection if는 위처럼 collection 내부에서 if 문을 사용하는 기능이다. 이 경우, if 내부 값이 true이므로 1이 intList의 내부값으로 들어간다.

String interpolation

문자열 내부에 변수 값을 넣고 싶으면 다음과 같이 $를 사용한다.

void main() {
  var name = "철수";
  var greeting = "hello nice to meet you, my name is $name";

  print(greeting); // hello nice to meet you, my name is 철수
}

collection for

void main() {
  var oldFriends = [
    "james",
    "erik",
  ];

  var newFriends = [
    "minsu",
    "chulsu",
    "yunghee",
    for (var friend in oldFriends) "old $friend",  // collection for with String interpolation
  ];
  print(newFriends);  // [minsu, chulsu, yunghee, old james, old erik]
}

collection for 기능을 이용해서 리스트 내부에 다른 컬렉션의 값을 추가할 수 있다.

Set

모든 값들이 하나씩만 존재하는 List이다.

Map

파이썬의 딕셔너리와 같다.

'Flutter > Dart' 카테고리의 다른 글

Dart- 클래스  (0) 2024.03.26
Dart- 함수  (0) 2024.03.18
Dart- 변수 정리  (0) 2024.03.17