flutterfreezedjson-serializable

How to use FreezedUnionCase.snake for model class


I am using freezed package as a code generator. My response from API as shown below,

first_name, 
last_name, 
etc..,

And I am defining my model class like this,

firstName,
lastName,
etc..,

If I use @JsonKey(name: 'first_name') then it works but I have to write this annotation for every field I have. Is there any way to set it global?


Solution

  • Please use this @JsonSerializable(fieldRename: FieldRename.snake) for snake_case convention

    For Example

    @freezed
    class City with _$City {
      @JsonSerializable(fieldRename: FieldRename.snake)
      const factory City({
        required int id,
        required String name,
        required int stateId,
        required String stateName,
      }) = _City;
    
      factory City.fromJson(Map<String, dynamic> json) => _$CityFromJson(json);
    }