How to use it
- Paste a real response from your API. A list of objects works too: the items are merged into one class.
- Set the class name and pick plain Dart,
json_serializableorfreezed. - Copy the code or download it as a
.dartfile and add it to yourlib/modelsfolder.
What it handles
- Null safety. A field that is
nullin your sample, or missing from some items in a list, becomes nullable (String?). Everything else isrequired. Tick “All fields nullable” for APIs you don’t control. - Ints and doubles. Numbers like
450and1299.5in the same field becomedouble, read with(json['total'] as num).toDouble()so a whole number from the server never crashes your app. - Dates. Strings in ISO 8601 format, like
2026-11-17or2026-11-17T09:30:00Z, becomeDateTime, read withDateTime.parseand written back withtoIso8601String(). A field becomesDateTimeonly when every sample value is a date; untick “Dates as DateTime” to keep them asString. - snake_case keys.
full_namebecomesfullNamein Dart, whilefromJsonandtoJsonkeep the original key. Keys that are Dart keywords (likeclass) get a safe name. - Nested objects and lists.
ordersbecomesList<Order>with its ownOrderclass, including lists of lists.
Plain Dart, json_serializable or freezed?
Plain Dart needs no packages and no code generation: what you see is the whole model. It suits small apps and quick prototypes. json_serializable output adds the annotations; rundart run build_runner build to generate the .g.dart file. It pays off in bigger apps with many models.
freezed output is an immutable class with copyWith, value equality (== andhashCode), toString and JSON, all generated. It uses the freezed 3 syntax (abstract class … with _$…). Add freezed_annotation and json_annotation to your dependencies and freezed, json_serializable and build_runner to your dev dependencies, then run dart run build_runner build. Choose it when you use Bloc or Riverpod, where value equality decides whether the UI rebuilds.
Questions
Is my JSON sent to a server?
No. The converter is JavaScript running in this page. Your JSON stays on your device, which matters when the sample contains real user data or tokens.
Why is a field typed dynamic?
Its value was null in every sample, or it mixed types (say a number in one item and a string in another). Paste a sample where it has a real value, or set the type by hand.
Can I trust the generated code?
The output is tested against the Dart analyzer and decoded back to the same JSON in round-trip tests. Still review it like any code you add, especially the nullability, because one sample can’t show every case your API returns.
Related guide: read Flutter JSON parsing: fromJson, null safety and code generation for the errors you will hit and how json_serializable and freezed compare.
Building a Flutter app and want it done right? I’m Muhammad Naeem, a Flutter developer with 10+ apps live on the stores. Tell me about your project.