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.

Inheritance

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.

Methods

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.

Enum

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


Generics

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!

2 Comments

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 *

seventeen − five =