2

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?

Hemal Moradiya
  • 1,715
  • 1
  • 6
  • 26

2 Answers2

2

Are you talking about FieldRename.snake?

import 'package:freezed_annotation/freezed_annotation.dart';

part 'event.freezed.dart';
part 'event.g.dart';

@freezed
class Event with _$Event {
  const Event._();

  @DocumentReferenceJsonConverter()
  @JsonSerializable(
    fieldRename: FieldRename.snake, // <---
  )
  factory Event({
    DocumentReference? reference, // reference
    String? eventTitle, // event_title
    String? eventDescription, // event_description
    String? eventLocation, // event_location
  }) = _Event;
}

json_annotation library documentation

1housand
  • 508
  • 7
  • 16
0

Json to Dart Model can automate JSON conversion to Freezed data class.

Arnas
  • 802
  • 1
  • 6
  • 14