tezvyn:

How do you model Product and safely parse JSON into List<Product>?

AI-drafted, machine-checkedSource: docs.flutter.devintermediate

Tests bridging dynamic JSON to Dart's type system. A strong answer uses an immutable Product with a factory constructor that validates fields and converts types, mapping over the list. Red flag: leaving everything dynamic or assuming perfect API data.

WHAT THIS TESTS: This question tests whether you can safely cross the boundary between dynamic runtime data and Dart's static type system. Interviewers want to see defensive parsing, null safety awareness, and clean separation between data models and UI logic. They also care if you understand factory constructors, immutability, and how to handle malformed API responses without crashing the app.

A GOOD ANSWER COVERS: First, define an immutable Product class using final fields and optionally const constructor. Second, provide a factory Product.fromJson(Map<String, dynamic> json) that does not blindly trust the payload. Inside fromJson, extract each field with runtime type checks like json['name'] is String, provide default values or throw a FormatException when a required field is missing or wrong, and explicitly cast values such as (json['price'] as num).toDouble(). Third, handle nested objects by delegating to their own fromJson methods rather than inlining everything. Fourth, convert the outer list by calling rawList.map((e) => Product.fromJson(e as Map<String, dynamic>)).toList() and wrap the entire operation in a try-catch or a Result type if you want to isolate bad rows.

COMMON WRONG ANSWERS: A major red flag is writing json['name'] as String without null checks or type guards, which throws at runtime if the server sends a number or null. Another mistake is making every field nullable and pushing validation into the UI layer, which spreads uncertainty throughout the codebase. Returning List<dynamic> instead of List<Product> defeats the purpose of the exercise. Some candidates also suggest using dart:mirrors or runtime reflection for serialization, which is discouraged in Flutter due to tree-shaking and code-size issues.

LIKELY FOLLOW-UPS: The interviewer might ask how you would handle a list of ten thousand items, which opens the door to compute or Isolate.run for parsing off the main thread. They may ask what happens when the API adds a new field, so you should mention versioning or using code generation with json_serializable or freezed. Another follow-up is partial failure: should one bad row crash the entire list or be filtered out, and how would you log the offending payload.

ONE CONCRETE EXAMPLE: Imagine a Product with String id, String name, double price, and int stock. The fromJson factory checks that id and name are strings, converts price via (json['price'] as num).toDouble(), and stock via (json['stock'] as num).toInt(). If any required key is missing, it throws FormatException('Invalid product: $json'). The top-level parser maps the API response and catches errors per item so that one malformed product does not prevent the rest from rendering.

Read the original → docs.flutter.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.