개발/Flutter

Freezed different key name, default value

dev_caleb 2022. 11. 30. 23:11
728x90

freezed 를 사용하고 있는데 변수명 다를 때랑 초기값 지정이 좀 고민이었다.

 

https://stackoverflow.com/questions/71562176/using-flutter-freezed-to-generate-code-to-parse-a-json-object

 

Using Flutter Freezed to generate code to parse a Json Object

I'm trying to parse some JSON from a file and decided to use Freezed to generate the code. The problem is that (as far as i can tell) there's no way to use a JSON object's name. So say I have the

stackoverflow.com

이걸 보고 문제 해결!

이런 식으로 anootation을 달아준다. Default는 초기값, JsonKey-name은 키 값 다를 때, Default를 넣어줄 때 required를 같이 쓰면 오류가 나니 주의!

 @Default([]) @JsonKey(name: 'other-links') List<OtherLink> otherLinks,

 

728x90