freezed는 Fluuter에서 불변 데이터 클래스와 패턴 매칭 기능을 쉽게 구현할 수 있게 도와주는 코드 생성 라이브러리이다.
class User {
final String name;
final int age;
User(this.name, this.age);
}
예시와 같은 간단한 모델을 실제 프로젝트에서 사용하려면
- == 연산자 오버라이드
- hashCode 구현
- copyWith 메서드
- toString
- JSON 직렬화
등등 번거로운 작업이 많이 발생한다. freezed는 이러한 작업들을 자동으로 만들어준다.
💁🏻 적용 가이드
1. 터미널을 이용한 설치
2. yaml 파일 직접 설치
3. plugin을 이용한 설치
✅ Freezed 설치 방법
1. 터미널을 이용한 설치
flutter pub add freezed_annotation
flutter pub add --dev build_runner
flutter pub add --dev freezed
# fromJson / toJson
flutter pub add json_annotation
flutter pub add --dev json_serializable
2. yaml 파일을 이용한 직접 설치
dependencies:
freezed_annotation:
dev_dependencies:
build_runner:
freezed:
json_serializable:
3. plugin 이용한 직접 설치
DartMappable plugin 설치
📍android studio Setting > plugins

원하는 파일 위치에 Json To Dartmappable 실행
임시 객체 값 할당 후 auto run build_runner, freeezed 체크 (formJson/toJson은 선택)
class Name 임시 값 할당 후 완료하면 필요한 라이브러리 자동 설치
자동 설치된 dependencies 값 pubspec.yaml 파일에서 확인 가능
💡 Freezed 설치 방법
- 터미널을 이용한 설치
→ 가장 직관적이고 빠르며, 단일 명령어로 의존성과 코드 생성기까지 설치할 수 있어 간편하다 - pubspec.yaml 수동 설정
→ 패키지 버전을 직접 지정할 수 있어 세밀한 버전 관리가 가능하지만, 매번 코드를 작성해야 하므로 번거로울 수 있다. - DartMappable 플러그인 사용
→ Android Studio 같은 IDE에서 플러그인을 활용해 시각적으로 의존성을 추가할 수 있어 초보자에게 친숙한 방법이다.
'Flutter' 카테고리의 다른 글
[Flutter] InkWell-GestureDetector 비교 (0) | 2025.04.18 |
---|---|
[Flutter] flavor를 이용한 실행 환경 분리 (0) | 2025.04.17 |
[Flutter] GestureDetector - Expanded 적용 오류 (0) | 2025.04.11 |
[Flutter] SizedBox-Container 비교 (0) | 2025.04.10 |
[Flutter] 폰트 적용하기 (0) | 2025.04.10 |