DEV Community

Cover image for Keep API Casing Out of Dart Field Names
YeLinAung for dust

Posted on Originally published at dust-codegen.pages.dev

Keep API Casing Out of Dart Field Names

Manual snake case JSON mapping beside a Dust Serde renameAll example

Dart apps read and write JSON all the time.

One small problem appears in many projects.

The API uses snake_case. The Dart model uses camelCase.

You can map every key by hand. That works, but the same casing rule is repeated in every model.

Dust Serde lets the model declare the rule once.

Use @SerDe(renameAll: SerDeRename.snakeCase). Dust applies that rule when decoding and encoding the model.

The field stays displayName in Dart. The JSON key stays display_name on the wire.

That keeps the model easy to read and keeps API naming rules out of handwritten mapping code.

Read the Dust rename strategies guide.

Dust #Serde #Dart #Flutter #CodeGeneration

Top comments (0)