(+) Data Layer - response to model
λ³ λ΄μ©μ μλλ° μΌλ°μ μΌλ‘ μμ£Ό μ°μ΄λ ννλΌμ μ 리λ₯Ό ν΄λλ€. APIν΄λΌμ΄μΈνΈλ₯Ό μ΄μ©ν΄μ μλ²μ ν΅μ μ νκ³ response λ₯Ό μμ λ°κ² λλ©΄ μ΄λ₯Ό κ°μ§κ³ λ΄λΆμμ μ¬μ©νλ model λ‘ μ»¨λ²ν μ ν΄μ€μΌ νλλ° μ΄ λ μΌλ°μ μΌλ‘ μ²λ¦¬νλ λ°©μμ λν μ 리μ΄λ€.
1. μμ‘΄μ±μΌλ‘ build_runner λ₯Ό μΆκ°νλ€.
build_runner λ μ½λ μμ± λ° λΉλ κ΄λ ¨ μμ μ νΈλ¦¬νκ² ν΄μ£Όλ ν΄μ΄λ€. μ¬κΈ°μ μΆκ°ν΄μ£Όλ μ΄μ λ μ΄λ Έν μ΄μ μ κΈ°λ°μΌλ‘ μλ΅μΌλ‘ λ°μμ¨ λ°μ΄ν°λ₯Ό λͺ¨λΈμ λ§€νν΄μ£Όλ κ³Όμ μ νμν μ½λλ₯Ό μλ μμ±ν΄μ£ΌκΈ° μν¨μ΄λ€.
build_runner
λ νλ¬ν°(Flutter) νλ‘μ νΈμμ μ½λ μμ±κ³Ό λΉλ κ΄λ ¨ μμ μ μλννκΈ° μν λꡬμ λλ€. μΌλ°μ μΌλ‘ μ½λ μμ± μμ μ λ°λ³΅μ μ΄κ³ λ²κ±°λ‘μ΄ μμ μΌ μ μμ΅λλ€.build_runner
λ μ΄λ¬ν μμ μ μλννμ¬ κ°λ°μκ° λ ν¨μ¨μ μΌλ‘ μμ ν μ μλλ‘ λμμ€λλ€.
build_runner
λ μ£Όλ‘ νλ¬ν°μ μ½λ μμ±κ³Ό κ΄λ ¨λ μμ μ μ¬μ©λ©λλ€. μλ₯Ό λ€μ΄, μ λ Έν μ΄μ (annotation)μ κΈ°λ°μΌλ‘ λͺ¨λΈ ν΄λμ€, λΌμ°νΈ μ€μ , 리μμ€ νμΌ λ±μ μ½λλ₯Ό μλμΌλ‘ μμ±ν μ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ κ°λ°μλ λ°λ³΅μ μΈ μμ μ μλμΌλ‘ μ²λ¦¬νμ§ μκ³ , μλνλ λ°©μμΌλ‘ μ½λλ₯Ό μμ±νκ³ μ λ°μ΄νΈν μ μμ΅λλ€.
build_runner
λbuild.yaml
νμΌμ ν΅ν΄ ꡬμ±λλ©°, μ£Όμ λͺ λ Ήμ΄λ λ€μκ³Ό κ°μ΅λλ€:
build_runner build
: μ½λλ₯Ό μμ±νκ³ λΉλν©λλ€.
build_runner watch
: μ½λλ₯Ό μμ±νκ³ λ³κ²½ μ¬νμ κ°μ§νμ¬ μλμΌλ‘ λΉλν©λλ€.
build_runner clean
: μ΄μ λΉλμμ μμ±λ μ½λλ₯Ό μμ ν©λλ€.
build_runner
λ μ£Όλ‘ μ½λ μμ±, 리μμ€ λ²λ€λ§, λ€κ΅μ΄ μ§μ λ±κ³Ό κ΄λ ¨λ μμ μ μ¬μ©λ©λλ€. μ½λ μμ± λꡬμΈFreezed
,json_serializable
,injectable
λ±κ³Ό ν¨κ» μ¬μ©νμ¬ νλ¬ν° μ ν리μΌμ΄μ μ κ°λ° μμ°μ±μ ν₯μμν¬ μ μμ΅λλ€.
2. μμ‘΄μ±μΌλ‘ json_serializable, json_annotation μ μΆκ°νλ€.
json_serializable μμλ΅μΌλ‘ λ°μμ¨ λ°μ΄ν° json μμ λͺ¨λΈλ‘ 컨λ²ν μ νλ ν¨μλ₯Ό μλμΌλ‘ μμ±ν΄μ£ΌκΈ° μν΄μ
@JsonSerializable()
μ μ¬μ©ν κ²μΈλ° μ΄ λ νμν μμ‘΄μ±μ΄λ€. μ¬μ©λ²μ 곡μλ¬Έμμ μ λμμλλ° μλμ μ§§κ² μ 리νλ€. κ·Έλ¦¬κ³ json_serializable μ μ¬μ©νκΈ° μν΄μ json_annotation λ μμ‘΄μ±μΌλ‘ μΆκ°ν΄ μ€λ€.
3. build_runner λ₯Ό μ΄μ©ν΄μ build λͺ
λ Ήμ΄λ₯Ό μννλ€.
μλμ κ°μ΄ @JsonSerializable() μ λ¬μμ£Όκ³ build_runner λ‘ λͺ λ Ήμ΄λ₯Ό μννλ©΄ μνλ μ½λκ° μμ±μ΄ λμ§ μλλ€.
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable()
class SignInResponse {
final String accessToken;
final String refreshToken;
final bool isProfileCompleted;
const SignInResponse({
required this.accessToken,
required this.refreshToken,
required this.isProfileCompleted,
});
}
sign_in_response.g.dart must be included as a part directive in the input library with:
part 'sign_in_response.g.dart';
κ·Έλμ part 'sign_in_response.g.dart'; λ₯Ό μλμ κ°μ΄ μΆκ°ν΄μ£Όκ³ μλ λͺ λ Ήμ΄λ₯Ό μ€νν΄μΌ .g νμΌμ΄ λ§λ€μ΄μ§λ€.
import 'package:json_annotation/json_annotation.dart';
part 'sign_in_response.g.dart';
@JsonSerializable()
class SignInResponse {
final String accessToken;
final String refreshToken;
final bool isProfileCompleted;
const SignInResponse({
required this.accessToken,
required this.refreshToken,
required this.isProfileCompleted,
});
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'sign_in_response.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
SignInResponse _$SignInResponseFromJson(Map<String, dynamic> json) =>
SignInResponse(
accessToken: json['accessToken'] as String,
refreshToken: json['refreshToken'] as String,
isProfileCompleted: json['isProfileCompleted'] as bool,
);
Map<String, dynamic> _$SignInResponseToJson(SignInResponse instance) =>
<String, dynamic>{
'accessToken': instance.accessToken,
'refreshToken': instance.refreshToken,
'isProfileCompleted': instance.isProfileCompleted,
};
dart run build_runner build
μλ μμ±λ μ½λλ₯Ό μ¬μ©ν΄μ μλμ κ°μ΄ factory λ©μλλ₯Ό μ μΈν΄μ£Όμ.
import 'package:json_annotation/json_annotation.dart';
part 'sign_in_response.g.dart';
@JsonSerializable()
class SignInResponse {
final String accessToken;
final String refreshToken;
final bool isProfileCompleted;
const SignInResponse({
required this.accessToken,
required this.refreshToken,
required this.isProfileCompleted,
});
factory SignInResponse.fromJson(Map<String, dynamic> json) {
return _$SignInResponseFromJson(json);
}
}
Last updated