Skip to content
Articles
Article

Your Dart model is right for one JSON response and wrong for the next

by Chanpenh Yorn dev.to

1 min read

Generating a Dart model from one sample response is fast, and it is usually wrong in a few places. Here is a product item from an API: { "id": 102, "name": "USB-C cable", "price": 5, "discount": 0.15, "tags": [], "note": "Ships in 2 days" } A model built from this item alone guesses: price: json['price'] as int, // 5 looked like an int discount: (json['discount'] as num).toDouble(), // never null…

Forum

Questions about this, or something to add?

Discuss on the forum

Report this · Wrote this? Claim it

FlutterX is supported by