0
0
Fluttermobile~20 mins

Model classes from JSON in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct JSON parsing in Flutter model
Given this JSON string representing a user:
{"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'],
    );
  }
}
Afactory User.fromJson(Map<String, dynamic> json) => User(id: json['id'], name: json['name'], email: json['email']);
BUser.fromJson(Map<String, dynamic> json) => User(id: json['id'].toString(), name: json['name'], email: json['email']);
CUser.fromJson(Map<String, dynamic> json) => User(id: json['id'], name: json['name'], email: json['email'].toInt());
DUser.fromJson(Map<String, dynamic> json) => 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.
ui_behavior
intermediate
1: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");
ARow(children: [Text(user.name), Text(user.email)])
BColumn(children: [Text(user.name), Text(user.email.toString())])
CColumn(children: [Text(user.name), Text(user.email)])
DColumn(children: [Text(user.id), Text(user.email)])
Attempts:
2 left
💡 Hint
Use a vertical layout and display the correct fields as strings.
lifecycle
advanced
2: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?
AdidChangeDependencies()
BinitState()
Cbuild()
Ddispose()
Attempts:
2 left
💡 Hint
Consider when you want to initialize state once before UI builds.
navigation
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?
ABoth B and C are valid ways to pass the model.
BNavigator.pushNamed(context, '/screenB', arguments: user);
CNavigator.push(context, MaterialPageRoute(builder: (_) => ScreenB(user: user)));
DYou cannot pass objects between screens in Flutter.
Attempts:
2 left
💡 Hint
Flutter supports passing data via constructor or route arguments.
🔧 Debug
expert
2:30remaining
Identify the cause of JSON parsing runtime error
Given this Dart code snippet parsing JSON:
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?
ANo error, code runs fine
BFormatException: Invalid JSON format
CNullPointerException
DTypeError: String cannot be assigned to int
Attempts:
2 left
💡 Hint
Check the type mismatch between JSON field and model field.