0
0
Fluttermobile~10 mins

Constructors and named parameters in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a constructor with a named parameter called 'name'.

Flutter
class Person {
  String name;
  Person([1]);
}
Drag options to blanks, or click blank then click option'
Arequired this.name
Bthis.name
Cname
Dthis.name = name
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use curly braces for named parameters.
Not marking the parameter as required when needed.
2fill in blank
medium

Complete the constructor to accept an optional named parameter 'age' with a default value of 18.

Flutter
class Person {
  int age;
  Person([1]);
}
Drag options to blanks, or click blank then click option'
Athis.age = 18
Bthis.age = 0
Cthis.age = 18, required
Dthis.age
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to mark default parameters as required.
Not assigning a default value properly.
3fill in blank
hard

Fix the error in the constructor to correctly assign the named parameter 'title' to the field.

Flutter
class Book {
  String title;
  Book([1]) {
    this.title = title;
  }
}
Drag options to blanks, or click blank then click option'
AString title
Brequired this.title
Cthis.title
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring parameter without 'this.' and trying to assign manually.
Omitting 'required' for mandatory parameters.
4fill in blank
hard

Fill both blanks to create a constructor with a named parameter 'color' that is optional and has a default value 'blue'.

Flutter
class Car {
  String color;
  Car([1] [2]);
}
Drag options to blanks, or click blank then click option'
Athis.color = 'blue',
Brequired
Cthis.color
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'required' with default values.
Not assigning default value properly.
5fill in blank
hard

Fill all three blanks to define a constructor with named parameters 'width' and 'height' both required, and an optional 'color' with default 'red'.

Flutter
class Rectangle {
  int width;
  int height;
  String color;
  Rectangle([1] [2] [3]);
}
Drag options to blanks, or click blank then click option'
Arequired this.width,
Brequired this.height,
Cthis.color = 'red',
Dthis.color
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'required' for mandatory parameters.
Not setting default value for optional parameters.