DEV Community

Cover image for Classes in Dart
Giuseppe Vetri for Codingpizza

Posted on

Classes in Dart

Hi! We've been learning about Dart for some weeks, if you've been following this series, you know we learned a lot of topics. In this post we're going to talk about Classes in Dart, the Classes are one of the basics of programming.

๐Ÿ“˜ What is a Class

A class is the object blueprint. Inside the class, we can find their properties, constructors, and functions. We already talked about functions in Dart, so we're going to skip that part.

Properties

The class properties are variables declared inside the class. For example, if we have a class named Car, the car color can be a string variable with the value "Blue."

Constructors

The constructors help us to initialize and set the values of our object. When we create a class and pass some variable as a parameter, we're using a constructor.

๐Ÿค” How to create a Class in Dart

To create a Class in Dart, we need to use the reserved keyword class and then the name of the Class. For example:

class Dog {

}
Enter fullscreen mode Exit fullscreen mode

With this, we can create any Dog we want. But there's more our Dog needs a name, so let's create a variable that assigns that name.

class Dog {
  String name = "Rocky";
}
Enter fullscreen mode Exit fullscreen mode

That's great! Now every time we create a new Dog, it's going to have Rocky as the name. Now we need to add our dog's ability to bark. For that, we need to create a function.

class Dog {
  String name = "Rocky";

  void bark(){
    print("Woof Woof! ๐Ÿถ");
  }
}
Enter fullscreen mode Exit fullscreen mode

We already have a blueprint for a Dog named Rocky who can bark. All we need to do is create an object from this blueprint is use the constructor.

var MyDoggo = Dog();
Enter fullscreen mode Exit fullscreen mode

The previous example shows us how we can create a Dog object using a default constructor. But what if we want to create another dog with another name? Not all dogs are named Rocky.

๐Ÿถ Creating uniques dogs with constructors

To assign the name of our Dogs, we need to create a Constructor inside the class, which can help us to initialize our Dog objects with their name.

class Dog {
  String name = "Rocky";

  Dog(this.name)

  void bark(){
    print("Woof Woof! ๐Ÿถ");
  }
}
Enter fullscreen mode Exit fullscreen mode

Now we can create our Dogs using the following code.

var doggo = Dog("Sparky");
doggo.bark();
Enter fullscreen mode Exit fullscreen mode

There are more Constructors. We're going to talk about those in the next CodingSlices Extended. Remember to follow me, so you don't miss it.

๐Ÿ• Finally, here is a spooky dog .

That's it

I hope you liked it. Do you want to learn more? I'm also creating new CodingSlices about Flutter on Instagram, feel free to follow me inย @codingpizzaย for more content.

I'm also writing an eBook, which is a basic course of Dart. It's about all you need to know to get started with Flutter.ย It's free, and you can sign-up here.

Now is your turn

You can try these concepts in IDE like Intellij idea community, which is free. All you need is to install the Dart plugin. Visual Studio Code or in some online editors like Dartpad.

Top comments (3)

Collapse
 
vicradon profile image
Osinachi Chukwujama

Hey @gvetri . I feel you should cover this part of your series more extensively. Talk about private and public members. Also, talk about getters, setters and required parameters.

Thanks for writing this series. You're doing great work.

Collapse
 
gvetri profile image
Giuseppe Vetri

Hey! Thanks for reaching out, Yeah I probably should, thanks for suggesting it!.

Collapse
 
jenniekibiri profile image
jenny

This is a really great article ๐Ÿ‘๐Ÿ‘