[Flutter] 22. 통신

김주희's avatar
Jun 08, 2025
[Flutter] 22. 통신
?

처음 세팅

의존성 추가

notion image
 

디렉토리 구조

notion image
 

main.dart

notion image
 

post_page.dart

notion image
 

화면

notion image
 

ViewModel 만들기

notion image

post_vm.dart

notion image
 

창고 데이터 타입 만들기 - 필드

  1. 클래스명은 페이지명과 동일하게 만든다.
  1. dto와 동일하게 만든다.
  1. Java의 dto에서는 내부 클래스가 가능하지만 dart에서는 내부 클래스가 불가능하다
    1. record 문법을 적용한다.
notion image
 

Dart record 문법

  1. record
    1. 작은 클래스나 튜플을 대신할 수 있는 구조
    2. 간단한 데이터 묶음이 필요할 때 사용
 
  1. 기본 Map 방식
    1. 문자열 키를 통해 값을 꺼낼 수 있다.
notion image
 
  1. Unnamed Record (이름 없는 레코드)
    1. 이름 없이 값만 나열
    2. 순서를 기반으로 필드에 접근 가능
    3. 예 - . \$1, . \$2
    4. 타입 추론으로 Dart가 자동으로 타입을 (String, int)로 인식
notion image
 
  1. Unnamed Record with Type Annotation (타입 명시한 레코드)
    1. Unnamed Record (이름 없는 레코드)와 동일하지만 타입을 명시적으로 작성
notion image
 
  1. Named Record (이름 있는 레코드)
    1. 레코드에 필드 이름 부여
    2. .name, .age와 같이 이름으로 접근 가능하다.
    3. ({String name, int age}) = 레코드 type
    4. (name: "데어", age: 30) = 레코드 value
notion image
 
  1. destructuring (레코드 분해)
    1. ("쌀", 40) = Unnamed Record
    2. 왼쪽에서 name, age 변수 두 개 선언과 동시에 값을 꺼내서 담는다. = destructuring (레코드 분해)
notion image
 
  1. 전체 코드
    1. void main() { final person = {"name": "코스", "age": 10}; print(person["name"]); // 출력: 코스 print(person["age"]); // 출력: 10 // 1. unnamed record (이름 없는 레코드) final user = ("코스", 10); // .\$1, .\$2로 각 필드에 접근 print(user.$1); // 출력: 코스 print(user.$2); // 출력: 10 // 2. unnamed record with type annotation (타입 명시) (String, int) user2 = ("메타", 20); print(user2.$1); // 출력: 메타 print(user2.$2); // 출력: 20 // 3. named record (이름 있는 레코드) ({String name, int age}) user3 = (name: "데어", age: 30); print(user3.name); // 출력: 데어 print(user3.age); // 출력: 30 // 4. destructuring (레코드의 값 분해해서 각각 변수로 선언) final (String name, int age) = ("쌀", 40); print(name); // 출력: 쌀 print(age); // 출력: 40 }
 

창고 데이터 타입 만들기 - 함수

 
 
 
 
 
 
 
 
 
 
 
 

PostModel
Share article

jay0628