Classes in Dart

Haider Ali
Complete Flutter Guide
Apr 8, 2024

Lesson#14

A class is a blueprint of an object. Classes are defined with the keyword class in Dart.

void main() {
Student st=Student("Ali", "Male", 20);
st.show();
}

class Student {
String? name, gender;
int? age;

Student(String name, gender, int age) {
this.name = name;
this.gender = gender ;
this.age = age;
}

void show() {
print('Name: $name, Gender: $gender, age: $age');
}
}

? with String and int means that variables defined are nullable. So the declaration Sting? name means that name is a variable that can hold String value or it can be null.

The keyword ‘this’ is used to refer to the object variable instead of the local variable.

Functions in the class are called methods. ‘show()’ is a method in the class Student.

What's’ next?

--

--