Challenge - 5 Problems
JSON Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct JSON parsing in Flutter model
Given this JSON string representing a user:
Which Dart code correctly parses this JSON into a User model?
{"id": 1, "name": "Alice", "email": "alice@example.com"}Which Dart code correctly parses this JSON into a User model?
Flutter
class User { final int id; final String name; final String email; User({required this.id, required this.name, required this.email}); factory User.fromJson(Map<String, dynamic> json) { return User( id: json['id'], name: json['name'], email: json['email'], ); } }
Attempts:
2 left
💡 Hint
Check the data types expected by the User constructor and how JSON values are accessed.
✗ Incorrect
Option A correctly maps the JSON fields to the User model fields with matching types. Option A misses the factory keyword. Option A wrongly converts id to string. Option A tries to convert email to int causing error.
❓ ui_behavior
intermediate1:30remaining
Displaying parsed model data in Flutter UI
You have parsed a User model from JSON. Which Flutter widget code correctly displays the user's name and email in a column?
Flutter
final user = User(id: 1, name: "Alice", email: "alice@example.com");
Attempts:
2 left
💡 Hint
Use a vertical layout and display the correct fields as strings.
✗ Incorrect
Option C uses Column to stack name and email vertically and displays correct string fields. Option C uses Row which lays out horizontally. Option C shows id which is int, causing error in Text widget. Option C unnecessarily calls toString() on email which is already a string.
❓ lifecycle
advanced2:00remaining
When to parse JSON in Flutter widget lifecycle
You receive JSON data asynchronously and want to parse it into a model and display it. Which Flutter widget lifecycle method is best to parse JSON after data is fetched?
Attempts:
2 left
💡 Hint
Consider when you want to initialize state once before UI builds.
✗ Incorrect
initState() runs once when the widget is inserted in the tree, ideal for starting async fetch and parsing. build() runs many times and should be pure. didChangeDependencies() runs on dependency changes, not ideal here. dispose() is for cleanup.
advanced
2:00remaining
Passing model data between Flutter screens
You have parsed a User model from JSON on Screen A. How do you pass this User object to Screen B when navigating?
Attempts:
2 left
💡 Hint
Flutter supports passing data via constructor or route arguments.
✗ Incorrect
Option A passes the User object directly via constructor. Option A passes it via named route arguments. Both are valid Flutter navigation methods to pass model data. Option A is false.
🔧 Debug
expert2:30remaining
Identify the cause of JSON parsing runtime error
Given this Dart code snippet parsing JSON:
The User model expects id as int but JSON has id as string. What error will occur?
final jsonString = '{"id": "1", "name": "Bob", "email": "bob@example.com"}';
final Map userMap = jsonDecode(jsonString);
final user = User.fromJson(userMap); The User model expects id as int but JSON has id as string. What error will occur?
Attempts:
2 left
💡 Hint
Check the type mismatch between JSON field and model field.
✗ Incorrect
The JSON has id as string "1" but User expects int id. This causes a TypeError at runtime when assigning string to int field. FormatException would occur if JSON was malformed. NullPointerException is not typical in Dart.