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

The "Variables & Types" 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 variable types can be explicitly declare or inferred. When the var keyword is used to declare a variable, Dart will infer the type and throw an error if a different type is assigned to the variable later in the application. All types in Dart are class-based objects.

Preview
Close

Transcript from the "Variables & Types" Lesson

[00:00:00]
>> In terms of variables and types, so how to declare a variable, there are two ways. If you know the type or if you wanna explicitly use the type, you use the type name as a prefix. So you say int data, similar to C, or Java. Okay, you start with a type and then the name.

[00:00:20]
But something that we have here is type inference. Type inference means that if you don't wanna define the type every time you can use bar as in JavaScript. But in this case bar needs something that we are assigning at that time. If I create a variable without a type, it becomes something of type dynamic.

[00:00:45]
That is actually pretty flexible, dynamic is like the default type in JavaScript, everything in JavaScript is kind of dynamic, okay? It can be anything actually. So it's a bad practice in that to create variables dynamic unless you have a real use case for using dynamic, okay? So when you use bar be careful to actually assign the value.

[00:01:07]
And if not, don't use bar and use explicitly the type that you wanna use on that variable. Okay, so you need to get used to these differences if you're coming from things like JavaScript. These are mutable variables, so everything you set a with the type or the bar keyword it's a mutable variable.

[00:01:29]
Then we have immutable variables. In this case instead of bar, we use final or we apply final to the type. And final is a variable that cannot be changed after it's set for the first time. That's an immutable variable, okay, make sense? Fine, and then we have constants that are using the const keyword, and we are going to use const a lot in flutter.

[00:01:54]
You will see that it's kind of annoying at one point, asking us to use const. So, in JavaScript, we do have const, okay? But what do you think, question for you, are they constants or immutable variables in JavaScript when you use the const keyword? It sounds weird because const seems constant, right?

[00:02:18]
That actually they are more like immutable variables. So first, what's the difference between an immutable variable and a constant? Does anyone know?
>> From a JavaScript side or from-
>> On any sides like a generic programming. Typically we have a difference between an immutable variable and a constant.

[00:02:40]
>> Const would be at compile time so it's set and it's more performant because it's already defined.
>> Exactly, so the constant is actually something that we define and we set at compilation time. In JavaScript, we don't have compilation time so that's why technically we kind of not using that idea, because we don't compile, okay?

[00:03:03]
An immutable variable, it's actually a variable, so it's actually a place in memory while our app is running, but after it was set, it cannot be changed. But I don't need to have a value at compile time for immutable variable, that's the difference. And for the constant, I actually need to have a value at compile time, okay, make sense?

[00:03:28]
Types, here, we have a mix between using lowercase and uppercase, but everything is an object. So don't get confused here with languages like Java, for example in Java, you have int and you also have integer with capital I. This is Java, okay? And this is a basic data type and this is an object, but actually here int is an object, everything is an object in Dart.

[00:03:57]
And we have some basic type with lowercase, they use lowercase, because they say that when you're coming from Java or from C, you're more used to use int with lowercase i. So we have int, double, bool, dynamic, that's anything. And we also have num, say num, that's a number that you don't define the type of the number, okay?

[00:04:21]
Actually this has to do with ROP, double and int, they're inheriting from num. So it's like a generic version, okay, of a number. And remember what happens when we export to JavaScript, the JavaScript runtime takes precedence there. So that means that a number, in fact, I'm not sure if you know the type of number in JavaScript.

[00:04:48]
I mean, if you use typeof of four, what you get in return number, okay, so it's a number. So that num kind of reference the number type in the JavaScript runtime, okay? Strings, you can use double quotes or single quotes, okay, both are working and as in JavaScript and they're doing exactly the same.

[00:05:14]
You will see that today, most of the design guidelines for Flutter are actually suggesting double quotes, just to pick one, okay, the same as in HTML. So in HTML you can use single quotes today if you want to, but it's kind of typical to see HTML with single quotes for attributes.

[00:05:34]
Well same here, you can create multiline, literal strings with triple, double quotes. So if I have a long description, you can use triple, double quotes and then you end up somewhere and then everything is part of the string. This is similar to other languages such as Kotlin or Swift, they have the same multiline experience.

[00:06:04]
It's similar to the backtick in JavaScript, when you open the backtick of string. And also within the string, one line or multiline string, you can use the dollar sign to actually replace templates with variables or with expressions. So for example, if I have the price here, it will say, the price is something like this.

[00:06:34]
For example, so in this case, this is going to say the price is 3 and it will then multiply the other price by 1.1, and it will convert the result of that expression into a string. Okay, so to the backtick or the template string in JavaScript, that's actually the name of that, yeah.

[00:06:55]
>> So type keywords are capitalized like string?
>> So the answer is not always, because int is not with capital i, double is not with capital i. Let's say, and again, I know what you're thinking, that's because it's not an object. No, that's not true, it's an object, everything is an object in Dart.

[00:07:19]
But they made the choice when they created the language to use lowercase on the basic types, integers, doubles. And why not the string? Because in Java, the string is a class and its capital S, so they tend to try to bring Java developers into Dart. So that's why a string is capital S.

[00:07:42]
If you're creating your own types, the guideline is to start with capital letters, okay? But let's say that you will see lowercase types only on these basic ones, bool, dabble, int, dynamic, boid as well, we haven't seen it yet and that's all. For the other types, they will be title case, so capitalized, but have in mind everything is an object anyway.

[00:08:12]
>> Now when you see a simple objects, they're still full objects?
>> They are still objects, full objects, so an int is an object, it's an object, there is a class behind. It's actually an object so that integer value has methods and properties like any other object. It's based on a class, it's not like an int in C or an int in Java, where it's not actually an object, here everything is an object.

[00:08:39]
You can actually even get inside how Ctrl+click, or Cmd+click on a Mac, you can actually get into the int. And you can see it's a class extending from num, from number, so it's a class. Because it's a class, it can contain methods constructors, and everything that we know on OOP.

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