본문 바로가기
Flutter

기본 자료형

by 패쓰킴 2026. 7. 22.
728x90
반응형

String

  String name = "철수"
  String email = 'hello@world.com';
  
  print(name + email); // 철수hello@world.com
  print(name + " " + email); // 철수 hello@world.com
  
  print("$name $email"); // 철수 hello@world.com
  print("name email"); // name email
  
  print("${name + email}"); // ${원하는 변수명 입력} -> 철수hello@world.com 
  
  print(email.split('@')); // [hello, world.com]

 

작은 따옴표와 큰 따옴표

- dart는 두 표현을 같은 문자열로 처리

- 공식 Dart 가이드에서는 작은 따옴표 사용을 권장

- 하지만 문자열 내부에 따옴표 자체를 포함해야 할 때는 구분하여 사용한다.

// 작은 따옴표 문자열 안에 작은 따옴표를 넣고 싶을 때 - 백슬래시(\) 필요
String sentence1 = 'It\'s a great day!'; // It's a great day!

// 큰 따옴표 문자열 안에 작은 따옴표를 넣고 싶을 때 - - 백슬래시(\) 불필요 (권장)
String sentence2 = "It's a great day!"; // It's a great day!
// 작은 따옴표 문자열 안에 큰 따옴표를 넣고 싶을 때 - 백슬래시(\) 불필요
String quote = 'He said, "Hello!"'; // He said, "Hello!"

 

Int / Double

  // int
  int age = 20;
  
  // double
  double longitude = 127.634324;
  
  // int -> double
  print(age.toDouble());
  
  // double -> int
  print(longitude.toInt());
  
  // 연산
  print(1 + 2); // 덧셈 = 3
  print(2 * 4); // 곱셈 = 8
  print(4 / 3); // 나누기 = 1.333...
  print(5 % 3); // 5를 3으로 나눈 나머지 = 2
  print(5 ~/ 3); // 5를 3으로 나눈 몫 = 1

 

Bool

  print(true); // 참 = true
  print(false); // 거짓 = false
  print(!true); // !는 not의 의미 = false
  
  // 비교 연산
  print(1 == 1); // == : 두 값이 같은지 비교
  print(1 != 2); // != : 두 값이 다른지 비교
  print(1 > 2); // false
  print("hello" == 'hello'); // true

 

List

  // 배열 생성
  List<String> fruits = ["바나나"];
  print('${fruits.length}'); // fruits 배열의 원소 개수 조회 -> 1
  
  // 추가
  fruits.add('딸기'); // [바나나, 딸기]
  fruits.add('배'); // [바나나, 딸기, 배]
  fruits.add(1); // error: fruits 타입이 String이므로 문자열만 추가 가능
  
  // 조회
  print(fruits[0]); // 배열에 0번째 원소 -> 바나나
  print(fruits[1]); // 배열에 1번째 원소 -> 딸기
  
  // 수정
  fruits[0] = "키위"; // 0번째 바나나를 키위로 수정 -> [키위, 딸기, 배]  
  
  // 삭제
  fruits.remove('딸기'); // 딸기와 일치하는 값 제거 -> [키위, 배]
  fruits.removeAt(0); // 0번째 원소 삭제 -> [배]  
  
  // 모든 타입을 담을 수 있는 배열 생성
  List<dynamic> buckets = [1, "문자", [1, 2]];
  buckets.add(true); // [1, 문자, [1, 2], true]
  print(buckets[2]); // [1, 2]
  print(buckets[2][0]); // 2번째 원소인 배열의 0번째 값 조회 -> 1

 

Map

Map<Key 타입, Value 타입>과 같이 타입을 명시

대괄호로 key와 value를 감싼다. {key:value}

  // {name: 철수, age: 20}
  Map<String, dynamic> person = {
    "name": "철수",
    "age": 20
  };
  
  // 조회
  print(person['name']); // 철수
  print(person['age']); // 20
  
  // 추가
  person['email'] = 'hi@mail.com'; // {name: 철수, age: 20, email: hi@mail.com}
  
  // 수정
  person['age'] = 10; // {name: 철수, age: 10, email: hi@mail.com}
  
  // 삭제
  person.remove('name'); // {age: 10, email: hi@mail.com}

 

728x90
반응형

'Flutter' 카테고리의 다른 글

기본 흐름제어  (0) 2026.07.31
var가 있고 없고의 차이  (0) 2026.07.21

댓글