Dart Classes and Objects

Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that encapsulates the data and functions together that can be accessed by creating an object. A class can refer to as user-define data type which defines characteristics by its all objects

Defining a Class in Dart

Dart provides class keyword followed by a class name is used to define a class; all fields and functions are enclosed by the pair of curly braces ({}). The syntax is given below.


class ClassName {  

A class definition can include the following


A field is any variable declared in a class. Fields represent data pertaining to objects.

Setters and Getters

Allows the program to initialize and retrieve the values of the fields of a class. A default getter/ setter is associated with every class. However, the default ones can be overridden by explicitly defining a setter/ getter.


Responsible for allocating memory for the objects of the class.


Functions represent actions an object can take. They are also at times referred to as methods.

Example for Declaring a class
class Car {  
   // field 
   String engine = "E1001";  
   // function 
   void disp() { 

The example declares a class Car. The class has a field named engine. The disp() is a simple function that prints the value of the field engine.

Dart Object

  • Dart is object-oriented programming, and everything is treated as an object in Dart.

  • An object is a variable or instance of the class used to access the class’s properties.

  • Objects have two features state and behavior.

  • Suppose a man is an object with a state (name, age, health) and behavior (walking, running, and sleeping).

  • Programming objects are theoretically similar to the real-life objects, they also have state and behavior.

  • An object is created from a template which is known as class.

  • The fields of the classes are stored as object states, whereas the method represents an object’s behavior.

Dart object definition can include the following


Describes the object. The fields of a class represent the object’s state.


Describes what an object can do.


A unique value that distinguishes an object from a set of similar other objects. Two or more objects can share the state and behavior but not the identity.

Creating Class Objects in Dart

  • After creating the class, we can create an instance or object of that class which we want to access its fields and functions.

  • The new keyword is used to declare class followed by the class name.

  • The general syntax of creating an object of a class is given below.


var object_name  = new class_name(<constructor_arguments>);  

Advantages of Objects

There are various benefits of using object-oriented programming. Below are the few benefits.


The source code of an object can be maintained individually and can hide from the other object’s source code.

Data - hiding

Using oops programming, the details of the internal functionality of code are hidden from the others. For example - Users only interact with the application, but they don’t familiar with the internal implementation.


We don’t need to write the same code again and again. We can use the object of class multiple times in our program.

Easy Pluggability and debugging

If any object is creating a problem in our program, and then we can replace it in our program and plug the new object as its replacement. The oops code can be easy to debug.


Dart represents data in the form of objects. Every class in Dart extends the Object class. Given below is a simple example of creating and using an object.

class Student { 
   void test_method() { 
      print("This is a  test method"); 
   void test_method1() { 
      print("This is a  test method1"); 
void main()    { 
   Student s1 = new Student(); 
This is a test method 
This is a test method1