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

The "Dart Basics" 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 walks through the Dart syntax and shares examples of common coding structures. Variables and functions are named using camel case. Private class properties are prefixed with an underscore. The syntax for declaring variables, conditions, and loops is similar to other languages like JavaScript or C#.

Preview
Close

Transcript from the "Dart Basics" Lesson

[00:00:00]
>> It's time to go and see some Dart code, okay, before actually getting into Flutter. So I'm going to get out of the slide for a minute and I'm going to use VS Code to open a folder that you have in the assets. So remember that we have in firtman.github.io/intro-flutter, we have a lesson, the first one, where you can download the workshop assets.

[00:00:27]
So in that ZIP file, you have a couple of images that we will use later. And also there is a folder with the name dart101, okay? So these are the basics of Dart. I want you to go. In fact, if you go to the next lesson here, it will just tell you to download the slides.

[00:00:44]
And next one, so the third lesson will actually tell you again to download the assets and open in Visual Studio or Android Studio, up to you, so be ready. I want you to open the folder dart101, okay? So I will use Visual Studio. It's the same if you wanna use Android Studio.

[00:01:04]
So I'm going to open a folder. And I'm going to look into my, The Assets > dart101. So I'm going to open that folder. In that folder, what we have is a couple of files, Dart files. And by the way, you can see at this point the extension of Dart files, it's .dart.

[00:01:27]
And here we can actually start playing with some Dart code and see errors in the compiler. Well, the first news that I have for you, I know that when I'm saying this, a lot of people will feel a little sad about this, but yeah, you'll get used to that.

[00:01:46]
Semicolons are mandatory in Dart. So if you're coming from Kotlin or Swift or other modern languages where the semicolon is on mandatory, you will need to get used to use semicolons again. So first, if you look at this, this looks like JavaScript, okay? So I have two variables, semicolons.

[00:02:10]
It actually looks like JavaScript. So we actually have the var keyword to define variables as in JavaScript, but it's like something completely different. So actually, the var keyword works similar to the var keyword in C#, if you have experience with that. So if you don't, don't worry, I will explain what that is.

[00:02:29]
So in JavaScript, you create a variable with var or now with let, if you want. And actually always, it's just a memory space where you can store values. And you can actually change the type of those values later. But here, we're not actually creating something like that. If you look at VS Code, if I hover my x variable, it says int x.

[00:02:55]
So what var is doing is inferring the type of my variable based on the value. And because I said equals to 1, it knows it's an integer, okay? So then I cannot just change like in JavaScript, this on a string, it will give me an error, okay? Because I cannot change them with a string because it's an integer, okay?

[00:03:21]
So anyway, we're going to talk about the types in a minute. In terms of conditionals, like if, whiles are pretty much the same as you're used to in JavaScript and other languages. So we use parentheses for Booleans. Boolean, in this case, conditions. So if you look at that code, I think it's pretty straightforward for most of you.

[00:03:43]
Print will actually print to the console. Have in mind that on different platform, the console means different things. Like on iOS and Android, there is an output console, but it's not actually the screen. On the web, the console is the browser's console. And if you're doing desktop applications, well, the console is the output console.

[00:04:04]
The term, you can see that in the output console where you're running your app, okay, but the place is different on different platforms. So as you can see here, I have, what is this? So you can guess what it is because it has parentheses. It's actually a function.

[00:04:22]
Why do I have a function here? Because I cannot have global code, like a global leaf. That's not possible in Dart. Everything must be in a class or in a function. So that's why here, to actually show you some code that will not giving us any errors, I need to put that into a function.

[00:04:41]
As you can see, by default, there is no function keyword. So in JavaScript, it will be function. Well, we don't need the function keyword, we just use the name, parentheses, and that's all. So playing with names, well, actually we use guidelines, the same guidelines that you're used to in JavaScript.

[00:05:01]
So for example, variable names. Constant names, we will get into that in a minute what's a constant. And function names, they use CamelCase. So we start with lowercase, and then we move up to CamelCase on our letter such as the M in message, okay? Everything is public in the package.

[00:05:21]
What's a package? In Dart, everything that we're doing in a project is by default a package, and we can have more than one package. So a package actually is kind of a folder, okay? And when you wanna use something from other package, you need to import the package.

[00:05:36]
This is similar to packages in Java or the namespaces in .NET, or even PHP, okay? So if you have used those languages, you probably know the idea of a package or a namespace. It's just a way to group, to contain different, let's say, objects that can be, for example, classes or functions or global variable.

[00:06:02]
So everything is public by default, but if you prefix your name with underscore, it becomes private automatically. So from other packages, even if you're importing the package, you cannot use or see that variable or function or class, okay? So it does not use visibility prefixes, such as protected, private, or public.

[00:06:27]
Everything is public, unless it starts with underscore. In that case, it's a private property, variable, function, or class. So data types such as a class will use TitleCase. So we'll start with CamelCase. So this is, again, similar to Java, to JavaScript, to many other languages. And for loops, we have the usual for, we have the usual while.

[00:06:53]
And also we have the for in that works similarly to other languages like for of or for each, depends on the language, okay? But here it's called for in. Be careful because in JavaScript, you also have a for in, but it's not the same. In JavaScript, for in will not loop through collections, it loop through properties of your object.

[00:07:17]
In a collection, every element is also a property. But anyway, you will also get into, not just if you have an array of ten strings in JavaScript and you use for if. You will get the ten elements and also properties of the array, more properties that you can set, that may not be strings, okay?

[00:07:39]
So here, this is more like the for each or for of of other languages.

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