0
0
Fluttermobile~20 mins

Classes and objects in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Classes and Objects
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Flutter widget?
Consider this Flutter code defining a class and using it in a widget. What text will appear on the screen?
Flutter
class Person {
  final String name;
  Person(this.name);
}

class MyWidget extends StatelessWidget {
  final Person person = Person('Alice');

  @override
  Widget build(BuildContext context) {
    return Text(person.name);
  }
}
AAlice
Bperson.name
Cnull
DError: name not found
Attempts:
2 left
💡 Hint
Look at how the Person class stores the name and how the Text widget uses it.
🧠 Conceptual
intermediate
2:00remaining
What does this Dart class constructor do?
Look at this Dart class constructor. What is its purpose?
Flutter
class Car {
  String model;
  int year;

  Car(this.model, this.year);
}
AIt creates a Car object but leaves model and year null.
BIt creates a Car object and sets model and year from parameters.
CIt creates a Car object with default model and year values.
DIt creates a Car object but requires setters to assign model and year.
Attempts:
2 left
💡 Hint
The constructor uses 'this' to assign parameters to fields.
lifecycle
advanced
2:00remaining
What happens when this Flutter StatefulWidget is created?
Examine this Flutter StatefulWidget code. What is printed when the widget is first inserted into the widget tree?
Flutter
class CounterWidget extends StatefulWidget {
  @override
  State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int count = 0;

  @override
  void initState() {
    super.initState();
    print('Init count: $count');
  }

  @override
  Widget build(BuildContext context) {
    return Text('Count: $count');
  }
}
ANo output printed
BCount: 0
CInit count: null
DInit count: 0
Attempts:
2 left
💡 Hint
initState runs once when the widget is created before build.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a Dart class with a named constructor?
Select the correct Dart code that defines a class with a named constructor called 'fromJson'.
A
class User {
  String name;
  User.fromJson(Map&lt;String, dynamic&gt; json) {
    name = json['name'];
  }
}
B
class User {
  String name;
  User.fromJson(Map&lt;String, dynamic&gt; json) =&gt; name = json['name'];
}
C
class User {
  String name;
  User.fromJson(Map&lt;String, dynamic&gt; json) {
    return name = json['name'];
  }
}
D
class User {
  String name;
  User.fromJson(Map&lt;String, dynamic&gt; json) =&gt; {
    name = json['name'];
  };
}
Attempts:
2 left
💡 Hint
Named constructors use the class name dot constructor name syntax with a body.
🔧 Debug
expert
2:00remaining
Why does this Flutter code cause a runtime error?
This Flutter code tries to display a person's age but crashes at runtime. Why?
Flutter
class Person {
  int? age;
  Person(this.age);
}

class AgeWidget extends StatelessWidget {
  final Person person = Person(null);

  @override
  Widget build(BuildContext context) {
    return Text('Age: ' + person.age!.toString());
  }
}
AThe '+' operator cannot concatenate String and int.
BThe Person class must initialize age with a non-null value.
Cperson.age is null, so person.age!.toString() causes a runtime error.
DStatelessWidget cannot have final fields.
Attempts:
2 left
💡 Hint
Consider null safety and the non-null assertion operator when accessing nullable fields.