import 'package:json_annotation/json_annotation.dart';
part '$NAME$.g.dart';
@JsonSerializable(explicitToJson: true)
class $CAP_NAME$ {
$END$
$CAP_NAME$();
factory $CAP_NAME$.fromJson(Map<String, dynamic> json) => _$$$CAP_NAME$FromJson(json);
Map<String, dynamic> toJson() => _$$$CAP_NAME$ToJson(this);
}
import 'package:freezed_annotation/freezed_annotation.dart';
part '$NAME$.freezed.dart';
part '$NAME$.g.dart';
@freezed
class $CAP_NAME$ with _$$$CAP_NAME$ {
const factory $CAP_NAME$({
$END$
}) = _$CAP_NAME$;
factory $CAP_NAME$.fromJson(Map<String, Object?> json) => _$$$CAP_NAME$FromJson(json);
}
Dart 3.0 이후
<aside> 💡 Dart 3.0 이후 Dart 의 sealed 키워드를 활용한 공식 문법을 사용한다면 아래와 같다. 하지만 코드가 여전히 많다. 그냥 아래의 freezed 활용하는 것을 추천
</aside>
sealed class $Result$<T> {
const factory $Result$.success(T data) = Success; // Option
const factory $Result$.error(String e) = Error; // Option
}
class Success<T> implements $Result$<T> {
final T data;
const Success(this.data);
}
class Error<T> implements $Result$<T> {
final String e;
const Error(this.e);
}
Dart 3.0 이후 (Freezed)
import 'package:freezed_annotation/freezed_annotation.dart';
part '$NAME$.freezed.dart';
@freezed
sealed class $CAP_NAME$<T> with _$$$CAP_NAME$<T> {
const factory $CAP_NAME$.success(T data) = Success;
const factory $CAP_NAME$.error(String e) = Error;
}
Dart 3 이후의 sealed class 와 함께 사용
switch ($condition$) {
case Success(:final data):
$cursor1$
case Error(:final e):
$cursor2$
}