Flutter takes advantage of classes and objects to help modularize many of your project functions. In this video, take a deep dive into the inner workings of these two vital Flutter functions.
and we've use them a little bit, it's time to do a deep dive and understand exactly how classes and objects work in Dart. So firstly, a class is simply just a blueprint and it's used to create all of the things that we're going to be showing in our app and the user is going to be interacting with. Those are the actual objects. But, because we can't see the objects until the app is running, we have to plan out how they will function and how they will work by creating these classes. A class has two really important things. One, are its properties. So, if we're building out a car, then that will be its color, or the number of seats it has, or the number of wheels it has, or the number of doors it has, or all sorts of things that are things to do with the characteristics of the object. And in Dart, these things that we've been referring to as variables, when they're inside a class, they're now called properties or fields. Now the other important part of a class that we need to define are its methods. So the things that it can do. The actions that the object, that will eventually get created, will be able to perform. In the case of our car object, that might be to drive or to brake or to turn around. And these methods are actions that the object will be able to do, that we're going to define in the blueprint, even before the object gets built. So, for example, you might see a class defined like this. Firstly, you have that class keyword, which shows that everything else that's coming afterwards, is going to be a blueprint. And then you have the name of the class, so in this case, we're building a car blueprint. And by convention, in Dart, as with many other languages, when you're creating a variable, say, here we've got number of doors, or when you're creating a function, say, here we've got drive. Those are all camel case, starting with a lower case word. But when you're creating a class to show that it's different, we always start off with a capital letter. So all the words in your class names are capitalized. Now, here's car class, and inside these curly braces, are the definitions for this car class. So this is the blueprint of exactly how our car will look and also how it will behave. And between the looks and the behavior, we end up with a class. So, in this case, we have just a single property, which is called numberOfDoors, and this defines a characteristic of the car, namely, how many doors it has. This car has five. This car also has a function called drive. And this defines a little bit of functionality for the car. So, namely, this car, when we call this thing called drive, is going to start turning the wheels, for example. Now, notice how inside a class, our variables are no longer called variables. And they're called properties or fields. Similarly, a function, when it's defined inside the curly braces of a class, it's now called a method. And not a function anymore. So, if you're new to programming, there's a lot of new words. And it's a good idea to build your own glossary of words, because it's just like learning a new language. When people refer to a method, you can think of them as just a function that's associated with a class. And when people are talking about a property, it's simply a variable that's associated with a class. And once you hear it often enough, and this is James. And he's also created from the human class, which is what this part means. We're going to the human factory and we're building a new human. And if I was to print his height, you'll see that it's exactly the same. He's also 15 centimeters tall. But that's probably not really true, right? Why should it be that when a new human gets created they all have the same default height? What if we wanted to initialize each object with a different value for some of these properties? How would we do that? Well, we already saw that in the last lesson. That we can use something called a constructor for our class. And to create a constructor, we would write the name of the class, so it's Human, and then we would create a set of empty parenthesis, and just as when we create a function, we provide some inputs into these parenthesis. When we create a constructor for a class, we can provide some inputs to our class. So we could say that maybe the starting height property, which is going to be a double, because our height properties are double, we'll give it name of startingHeight, and now inside the curly braces, we can assign the starting height to the height. And height can start out without a value. It'll start out being null. But when we create our new human, we have to provide a starting height. And then we can say that the human's height property will be set to whatever the starting height is that we provided. So, now we get some errors here because it needs a starting height and we haven't given it one. So, let's add that in. SO we could say that Jenny is a human who starts out being 15 centimeters, but James starts out being a human who is, maybe he's a bit taller, maybe he's 20 centimeters. So now, when we click Run, you can see that Jenny's height starts out 15, and James' height starts out at 20. And our code now makes a little bit more sense, because why should a human have a default height, right? They don't all get created with the same height. And just as we could make the input or the parameter, a named parameter in our functions, we can do the same in our class constructor. So we can add a set of curly braces, around these inputs to say that when we use it, when we create our new human object, we have to specify that name. So startingHeight and then a colon. So, this way it's a bit easier on the eye and you can see that I'm creating a new human and I'm providing a starting height for that human. As I said earlier, our classes can have properties, and it can also have methods. Our methods are simply functions that are defined inside a class. If we were to create a method, let's say, I don't know, talk, right? To get our little human to say something. We can tell it what to say, so we'll create a string input called whatToSay and then inside the talk function, or in this case the talk method, because we're talking about a function associated with a class, We can say that we'll just print the string that is told to say. So that means we can tell our new humans, say for example, let's get rid of James. Let's just say Jenny, talk. And the thing that you should say is "Why is the sky blue?" Now, when we run our code, we print Jenny's height and we get Jenny to talk. And you can see that by calling this method talk, then this object actually performs that action and it prints out the thing that we want it to say into the console. This is now a method that's associated with the human class. And just as we use the dot notation to access the property height, We're also using the dot notation to trigger the talk method. So, when we're working with objects, you see the dot all over the place and it just refers to saying this object do this, or this object get me it's property. So, we now have a pretty simple class. And this class is our blueprint for how a human object should behave and what characters it should have. So, all of the things that a human object, once it gets created, should be able to do, and what it should be. You can of course make this as complex as you need it to be, but in essence, we're simply creating a plan for how this object should behave or how it should appear. And we use it by creating it as a new object and we can create as many of these as we need to and get them to interact with each other, or get them to do specific tasks. And so this is how we would create our classes and objects. But why exactly do we need these classes and objects? To find out about that, I'll see you on the next lesson.
This course was created by London App Brewery. We are pleased to host this content in our library.