Check out a free preview of the full Cross-Platform Mobile Apps with Flutter course

The "Classes" Lesson is part of the full, Cross-Platform Mobile Apps with Flutter course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano explains how classes are created in Dart. Classes can have a traditional primary constructor or utilize any number of named constructors which are typically prefixed with "from". There are no interfaces in Dart but developers can create abstract classes.


Transcript from the "Classes" Lesson

>> So, the last part that I have on this section is to talk about classes. I mentioned this is OOP, okay? Object oriented programming. So Dart is actually an object oriented programming language. So that means everything is a class. So if you look at this class declaration, I have a person class, we will talk about imports later, but anyway.

I have a class here that has a property, integer, id. And then it has a function, a method, no function keyword. So this is pretty straightforward, okay? No big deal, I get that most of you will get the syntax. But about creating objects of Dart, you can use the new keyword as you're used to in JavaScript and other languages, or you can omit the new keyword, like in other languages as well such as Kotlin or Swift.

So actually here you can see it works on both cases. So p and q are two person objects and it's exactly the same, okay, but you will see that this is the Flutter way. So when we have a Flutter project, there is a linter included in VS Code and also in Android Studio.

The linter will complain that you shouldn't be using the new keyword. I mean, it works but it will tell you, hey, we are not using this in Flutter. So it's a good idea then to get used to not use the new keyword to create new instances of a class, okay?

Make sense? Just that. What about constructors? So constructors, we have a primary constructor, okay, a primary constructor. And then we have named constructors, okay? I have a typo here, constructors. So what is a primary constructor? It's the constructor that you have with a class name, okay, similar to other languages like Java.

We don't use the constructor keyword like in JavaScript, okay? So it's a function with the name of the class. And you can receive no arguments, one argument, more argument, named arguments. It's up to you, but you cannot create another one. So we have only one primary constructor, okay?

If you wanna create more of these constructors, you need to create a named constructor, and this is how you do that. So you use the name of the class, a dot and a name, and typically we start the name with from. So you create a request from the network, and you receive a URL or wherever, okay?

Well, I should do this. And you create another request from wherever, okay? So that's the idea, so you have one primary constructor and then you have these named constructors. So what about, let me remove this for a second. And let's see this behavior. So it's actually pretty common when you're doing OOP, object oriented programming.

You're receiving the constructor arguments and assign those arguments into properties. This is exactly what I'm doing. I have an id, and I'm saving that id in our property and receiving a url, and I'm saving that url in my property. Because this is actually so common, we have a shorthand here in Dart.

The shortcut is that instead of using here, id, we use, and here this.url, if I do this, I don't need the assignment, okay? So just by saying, this.url, it's going to be that property. It's going to assign that property automatically, makes sense? And if your contractor has no more code, we can even end the contractor there with semi column, so we replace curly braces, the code block, with a semicolon.

Okay, make sense? So, it's just that. Also, when you do this, you can get rid of the type, because the type is going to infer from the property declaration. Make sense? So yeah, it look fine. And now if you want more constructors, well, it's Request.from, and you put a name.

So for example, from, we are going to create a request from a URL. So, and then you receive here a url. And if you want, you can call the other contractor as well with colon, we will see that in the next sample. So I don't wanna mess with this.

So now if we go back to your original sample, the code that you have, okay? If you look at this named constructor fromData, it says, this.url. So it means it receives the id and the url, and it's saving the values in the properties. And there is no code, but if you want, you can have a code block and do something else.

Okay, makes sense? Yep.
>> Could you receive this with optional thing?
>> Yeah.
>> I'm just wondering-
>> But in this case here, they must be optional.
>> I'm just wondering about exactly. I'm wondering if this is would assign it automatically.
>> What do you wanna do? If you wanna-

>> Just optional like-
>> I mean, can receive here optional, like-
>> No, but with this, can you do with this optional or not?
>> No, because it's not optional here. So if you use this, if you use the shorthand, it must be the same type as the property.

Because if not, it's not a property. It's a different value. And then, you can receive an optional, and then you ask if the id is not null, then you do it manually, okay, like this, and if not, you apply the full one. I don't know, you can use ternary operator if you want for this as well.

But yeah, you cannot use a shorthand in that situation.
>> Why would you default ie to zero?
>> Just because I was playing with the idea of putting an option, it doesn't make any sense, yeah.
>> So is there a notion of a default constructor?
>> So-
>> You can't create requests right now without assigning it, right?

Well, now you can, from data you can.
>> Let me see if I get what you're saying. So right now, I can request with id and url or calling this named constructor. So, how do you call the named constructor? Well, I need to create a function to actually play with this.

And if you wanna create a variable, okay, let's create a request. I can make a request with a default constructor or fromData. And because this accepts null, I can pass null, actually. So that's the difference, and null will get here as null and it will save as zero.

Okay, make sense? Something that it's interesting in Dart is that when you are inside a conditional asking for not null, we know for sure that it's not null here, okay, in this code block. That means I don't need to unwrap the optional. Remember that we had to do that, for example, for the length or whatever.

I don't need to actually do that here. In fact, if I do that, I get a warning saying, hey, there is no effect here because the receiving can't be null. And you say, but it can be null. I mean, the variable can be null. But we are in the if, we are in the condition where we know it's not null.

Fortunately, we don't need to do this. That's good, that's good news. In other languages, that's not possible. So you always need to unwrap the optional. So here the compiler is smart enough to know that you are in, let's say in a path, in a branch, where your code makes that ID mandatory and you know it's there.

So you don't need to deal with the optional, with the null safety operators. Okay, make sense? A more advanced idea that I can tell you at this point regarding named constructors is you can make them a factory, okay? So what's the idea when you make them a factory?

We're going to see one later. When you make them a factory, it's not actually a constructor. It's a factory, okay, so what's the difference? It's actually using the abstract factory design pattern from the original Gang of Four, if you have ever played with those design patterns, OOP design patterns.

Actually, a constructor will always return something of type request. I don't have a return here. So in a constructor you don't return yourself. You don't do this, okay? Well, when you have a factory, you need to return an option. And the idea of a factory, the factory can return null.

The factory can return an object of your type, or sometimes the factory can return an object of a sub-type. For example, based on the arguments I decide to return not a request but an HTTP request or a socket request, that is a subclass of request. So a factory can return subclasses, objects of a subclass, or null.

Because the factory will say no, I'm not gonna construct an option, because I know the arguments are wrong or something happens, okay? So we can also create factories using this. Inheritance work like this with extends, like in Java, in JavaScript. In this case I create a constructor receiving two arguments.

And I'm extending from the super constructor, this is how you call the super constructor. And I don't have a code, but I can also open code here and do something else. Okay, so if you end with a semi colon, you don't have a code block, if not, you have a code block.

And you can use named parameters, for example, in the constructor. And we are going to see these a lot in Flutter. Flutter is full of named constructors everywhere. Okay, it's the same as a function, because a constructor is just a function.
>> So for the factory, how do you call it, is it like a static method?

>> It's like a static method, yeah. It's the same as the constructor, actually.
>> Okay, I'm good.
>> Question from the chat.
>> When should you use classes and when interfaces? Most of the time I use interfaces because most of the object-oriented code is back-end specific or the application just needs to display and manipulate data but the back end needs to follow programming best practices.

>> Okay, let's see, first, do we have interfaces here? So if I type interface, and i do something like this, actually it will complain. It says, variables, you can define const, final, var. So actually we don't have interface. So if you're coming from Java or other language, you don't have interfaces here as the idea.

So, there are many other design patterns that we use here. I'm not saying that you don't have a way to emulate interfaces. But let's say that in Dart, you will most of the time be creating classes. We don't have something simpler such as structures, or something to follow protocols.

There are many design patterns that you can use, but most of the time, we're going to be creating classes in Dart.
>> Is from required?
>> Are you talking about this, like if you wanna name your constructor differently? No, it's not required. It's actually part of a guideline, a style guideline.

So now you know that when you wanna crate object, use the class of the object, dot, and you see if there is a from something, but it's actually not mandatory. We are going to see later in Flutter that one of the widgets that we're going to use, a widget is a UI class, has something known as a builder.

And you will see that it's not actually using all the time that from prefix.
>> You said in the named constructors.
>> Is in?
>> From in the named constructors.
>> So from is actually part of the, it's like a function. It's a function that will create an instance of your request, and it will execute your code also, okay?

But this is a function name. So anything that goes in a function name will work, but we tend to use the from keyword, remember, this is if you wanna have more than one constructor. The default constructor is this one, mainly called primary constructor or default constructor. Then you can have more constructors with names.

They work like when you're having a static method in other languages returning objects, like a factory kind of.
>> How about in abstract class?
>> Okay, so if your question is, if I can create abstract classes? Yes, I can. So I have AbstractRequest, and of course, what's the difference?

You cannot create the instances of an abstract class or put a parenthesis, my bad. And, of course, you can extend from an abstract class and create an actual concrete class that you can use. So yeah, we do have abstract classes, the same as in Java or other languages with that idea.

Are we going to use that? In, Flutter, no, unless you are creating your own data model with abstract classes. No, we don't see that a lot, but it's there, okay? So if you wanna use it, it's there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now