What does `this.name` in constructor mean?
this.name is shorthand for this.name = name in the constructor.
Object-oriented programming basics
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Classes are blueprints for creating objects. Everything in Flutter is built using classes, so understanding them is essential.
1class User {2// Properties (fields)3String name;4int age;56// Constructor7User(this.name, this.age);89// Method10void sayHello() {11print('Hi, I am $name');12}13}1415// Using the class16var user = User('John', 25);17print(user.name); // John18user.sayHello(); // Hi, I am John
Dart allows you to create multiple constructors for a class using named constructors. This is useful when you need different ways to create the same type of object.
1class User {2String name;3int age;45// Default constructor6User(this.name, this.age);78// Named constructor9User.guest() : name = 'Guest', age = 0;1011// Named constructor from JSON (very common!)12User.fromJson(Map<String, dynamic> json)13: name = json['name'],14age = json['age'];15}1617var guest = User.guest();18var fromApi = User.fromJson({'name': 'Jane', 'age': 30});
`User.fromJson()` is how you convert API responses to Dart objects. You will use this pattern frequently.
Getters and setters let you control access to an object's properties. They allow you to add logic when reading or writing a value.
1class Rectangle {2double _width;3double height;45Rectangle(this._width, this.height);67// Getter - read the private field8double get width => _width;910// Getter - computed property11double get area => _width * height;1213// Setter with validation14set width(double value) {15if (value > 0) _width = value;16}17}1819var rect = Rectangle(10, 5);20print(rect.area); // 50.0
Model classes should be easy to create, validate, and maintain:
Strong model design prevents scattered data bugs later in larger projects.
Object oriented programming basics. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Build confidence with basics first; speed comes later.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
this.name is shorthand for this.name = name in the constructor.
Named constructors use ClassName.name() syntax.
The getter computes area as width * height = 5 * 3 = 15.
Dart uses _ prefix for library-private members. There is no private keyword.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue