Lets study about one of the most important part of a modern day language, without which these days a programming language is not complete, classes! Classes in Dart are simple yet they bring many new tricks to that table.

Classes in Dart

Here is how you declare a simple class in Dart and create an instance of it.

Now we will add instance variable in the class along with a constructor, lets add a name field and a age field for the Dog class.

Dart provides a syntactic way to define the plain old constructor. Here is how.

As you can see we have written a one line constructor, the first parameter value will be set to name and the second parameter value will be set to age.

Named Constructors

Dart provides another way to define a constructor, called named constructor.

As you can see we have provided a name for the constructor. All it does is add more clarity when using different constructor.


You can inherit other classes in Dart using the extend keyword.

Here our Pug class inherits from the Dog class and calls the constructor of the Dog class with appropriate parameters using the super keyword.

You can also call other constructor within in the same class by using the keyword this after the colons( : ).

Here we create two named constructors that take in only the name of dog and call the default Pug constructor.


Method in a class are just similar to defining methods independently in Dart.

Overriding methods is just as simple as well.

Getter and Setters

By default any variable you define in a class is accessible by the just referring to the name of variable on the object, e.g. dog.name, value in name can be assessed and assigned directly. But sometimes you want custom getter and setter for a property, in Dart you can use get and set keywords do define custom getter and setter.

Now you might think that the name is still visible and can be changed at will, let’s fix that my making name property private.

Controlling Accessibility

By default every property and method you define in a class is public and can be accessed directly. In Dart you have the ability to make any variable or method private by just appending ‘_’ in front of its name. Let’s make the name property private.

Abstract Classes and Methods

You can make an abstract class in Dart using the abstract keyword.

You only need to provide the keyword before the class declaration, and don’t need it for methods. For methods just provide the signature and leave out the implementation.

Static Methods

To make a field/method static just prefix the keyword static before its declaration.


Dart has support for enums and use like everything else, you will be familiar with them if you come from a language say Java.

Never miss a post from TheTechnoCafe


Dart has full support for generics. Lets say you are writing a class that just holds data in it and you want to make it such that it can hold any kind of data. Here is how you will write that class using generics.

Next Tutorial >> Just enough Dart for Flutter – Tutorial 04 – Asynchronous and Libraries.

Previous Tutorial << Just enough Dart for Flutter – Tutorial 02 – Control Flow and Exceptions.

Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!


Palaniraja · July 10, 2018 at 4:16 pm

Thanks for the dart posts. There were lot of typos for String as “Stirng”

Surya · January 8, 2019 at 7:45 am

I really enjoyed your crash course on flutter. This really helped me get started right away with flutter. Please do more of these flutter series!. Also more on dart topics please! thank you so very much!

Ayman Ahmed · January 8, 2019 at 8:16 pm

Thank you for this great post. Could you please add information related to class constructor, and the colon(:) operator after the contractor. It is confusing, I just understood it after reading below thread:


Maruf Alom · June 21, 2019 at 3:43 pm

Nicely written. Would you be kind to write another article about Generics?

Koustubh Kulkarni · January 8, 2020 at 6:44 am

Nice explanation in simple and easy to understand examples !
Thanks !

Tech · January 21, 2020 at 3:17 pm

class Pug extends Dog {
Pug(String name, int age): super(name, age);

Pug.small(String name): this(name, 1);

Pug.large(String name): this(name, 3);
Thanks for the tutorial.And please can you explain more about Pug.small and :

Just enough Dart for Flutter – Tutorial 02 – Control Flow and Exceptions - TheTechnoCafe · June 12, 2018 at 7:03 pm

[…] Next Tutorial >> Just enough Dart for Flutter – Tutorial 03 – Classes and Generics… […]

Just enough Dart for Flutter - Tutorial 04 - Asynchronous and Libraries - TheTechnoCafe · June 12, 2018 at 7:05 pm

[…] Previous Tutorial << Just enough Dart for Flutter – Tutorial 03 – Classes and Gene… […]

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − 16 =