How to Create A Class In Dart?

8 minutes read

To create a class in Dart, you can use the class keyword followed by the name of the class. Here's an example of a basic class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Person {
  // Properties
  String name;
  int age;
  
  // Constructor
  Person(this.name, this.age);
  
  // Methods
  void speak() {
    print("Hello, my name is $name and I am $age years old.");
  }
}


Let's break down the components of this class:

  • The Person class is declared using the class keyword followed by the class name.
  • Inside the class, you can declare properties. In this example, we have name and age.
  • The Person class also has a constructor. Constructors are used to create instances of the class. In this case, it takes name and age as parameters and assigns them to the corresponding properties.
  • We've defined a method called speak() which prints a message using the print() function. It uses the properties name and age to display the information.


To use this class, you can create an instance of it like this:

1
2
3
4
void main() {
  Person person = Person("John", 25);
  person.speak();
}


Running this code will create a Person object with the name "John" and age 25 and call the speak() method, resulting in the output: "Hello, my name is John and I am 25 years old."

Best Dart Books to Read in 2024

1
Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

Rating is 5 out of 5

Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

2
Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Rating is 4.9 out of 5

Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

3
Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

Rating is 4.8 out of 5

Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

4
Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

Rating is 4.7 out of 5

Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

5
The Dart Programming Language

Rating is 4.6 out of 5

The Dart Programming Language

6
Mastering Dart: A Comprehensive Guide to Learn Dart Programming

Rating is 4.5 out of 5

Mastering Dart: A Comprehensive Guide to Learn Dart Programming

7
Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

Rating is 4.4 out of 5

Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

8
Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition

Rating is 4.3 out of 5

Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition


What is a class in Dart?

In Dart, a class is a blueprint or a template for creating objects. It contains data members (variables or fields) and functions (methods) that operate on those data members. Classes in Dart are used to define the structure and behavior of objects. Objects are instances of classes, which means they are created based on the defined class and can have their own unique values for the data members. Classes also support inheritance, allowing one class to inherit the properties and methods of another class.


How to define a static property in a Dart class?

To define a static property in a Dart class, you can use the static keyword. Here's an example:

1
2
3
class MyClass {
  static int myStaticProperty = 10;
}


In this example, myStaticProperty is a static property in the MyClass class. It can be accessed using the class name directly, without needing to create an instance of the class:

1
print(MyClass.myStaticProperty); // Output: 10


Static properties are shared among all instances of the class and can be accessed and modified by any instance or static method of the class.


How to achieve polymorphism in Dart classes?

To achieve polymorphism in Dart classes, you can follow these steps:

  1. Create a base class: Start by creating a base class that defines a common interface or behavior for your objects. This base class acts as a blueprint for derived classes.
1
2
3
4
5
class Shape {
  void draw() {
    print('Drawing a shape...');
  }
}


  1. Create derived classes: Create derived classes that inherit from the base class and provide their own implementation of the common interface. Each derived class can override methods and add new methods or properties as needed.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Circle extends Shape {
  void draw() {
    print('Drawing a circle...');
  }
}

class Square extends Shape {
  void draw() {
    print('Drawing a square...');
  }
}


  1. Use polymorphism: Use the base class type to create objects of derived classes, and then call the common method on these objects. The method implementation to execute will depend on the actual type of the object.
1
2
3
4
5
6
7
void main() {
  Shape circle = Circle();
  circle.draw();  // Output: Drawing a circle...

  Shape square = Square();
  square.draw();  // Output: Drawing a square...
}


In this example, even though the objects circle and square are declared with the type Shape, the actual implementation of the draw() method executed depends on the actual type of the object (i.e., Circle or Square). This behavior is known as polymorphism.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To send emails via Gmail from Dart, you can use the mailer package. Here are the steps to achieve this:Set up your Dart project by creating a new Dart file and importing the necessary packages. import 'package:mailer/mailer.dart'; import 'package:m...
Dart DevTools is a powerful tool for debugging Dart applications. It provides a comprehensive set of features to help you understand and analyze the behavior of your code during runtime. Here's an overview of how to use Dart DevTools for debugging:Installa...
To read user input in Dart, you can use the standard input/output methods provided by the dart:io library. Here's how you can do it:Import the dart:io library: import 'dart:io'; Create an instance of the stdin object from the io library: var input ...