Now in the last lesson, we saw inside dartpad when we tried to change the variable that's holding a
string namely a string of characters, so basically text.
And we tried to make it hold a number instead, or an integer.
It wasn't really happy with that and it didn't want to do it for me.
In order to understand what's going on, we have to understand Dart data types.
Dart is what we call a statically typed language.
What does that mean?
Well let's say we create a variable called myVar. It kind of acts a bit like one of those children's
toys which has different shaped holes and you have to fit the right shape inside the right hole.
So for example, if I had something that was circular, then that would go through the circular hole.
But if I had something that was a triangle for example, then it won't go through the hole. And it's very
similar
when we have statically typed languages like Dart. Essentially when I create a variable and I say that
this variable can only hold pieces of text,
then if I try to put a number into this variable it won't accept it.
It says I can't mix up my types.
What are these types that we're talking about?
Well they're essentially types of data.
So we've got strings, which are essentially a string of characters like a string of pearls, but we've
strung together some characters instead.
And in Dart, its convention to have a single quote around the piece of text to represent a string.
So we know that if we don't have single quotes, the computer will think that we're writing code. But if
we do have the single quotes, then it'll know that we're trying to specify a string and it doesn't treat
it as code.
Now there's also int, which is short for integer. And integers are basically just whole numbers.
They can be positive or negative, but they can't have a decimal point.
So if you wanted a decimal point in Dart, you would be working with something called a double.
And this is a data type that allows you to have a decimal point
and it will hold your data with all those numbers after the decimal point.
Now the last data type that you'll see commonly occurring, is something called a bool which is short
for a boolean. And it's a data type that can only hold one of two values, either true or false.
And all of these data types together are known as primitive data types.
So they're the most basic fundamental building blocks of any computer program.
And we're going to use them as we build up more and more complex apps to store more and more complex
data. Coming back to this idea of Dart being a statically typed language.
It means that at the point when we create the variable, it will get a data type and at a later date, we
can change what's stored inside the variable as long as we don't change the data type. Heading back into
our dartpad
let's delete everything inside our main function. And this time let's create some other variables.
We're going to create a variable a, and we're going to assign this to the value
hello.
Now at the moment when I double click on this a variable ,the dartpad very helpfully points out that
this is a string.
This variable holds data that has to be of the type string.
So
hello
works or bye
or anything that's inside a set of single quotes will work just fine.
But I can't change the data type of this variable.
I can't for example change it to 123.
Now this is what we mean when we say that Dart is a statically typed language.
If it was a dynamically type language and there's many of those out there, then this would be a perfectly
valid thing to do.
And it won't complain at all.
It'll just silently suck it up and probably judge you,
the human whose programming this, for being so indecisive.
But it won't actually have a problem.
And one of those languages is JavaScript.
So if I straight up just take these two lines of code that we've already seen leads to all sorts of
problems in Dart, and I put it inside a JavaScript playground such as jsbin, and if I console log, which
is the JavaScript equivalent of print, the value of the variable a, then it'll happily print 123
in here.
And if I put it here, then it'll print hello.
So you can see that these are completely different data types and being able to change the data type
that a variable holds, makes JavaScript a dynamically typed programming language.
Now there are pros and cons of each type of programming language. Dynamically typed languages are more
flexible.
You can change things on the fly, where statically typed
programming languages are generally safer. You're less likely to run into problems because you forgot
that, 'oh a was actually meant to be a variable that only holds strings'.
It'll actually enforce it for you.
Now with Dart though, there is a way of turning it into a dynamic programming language. And it might actually
trip you up at some point. here in this line.
We've created our variable a and we've set it to equal hello.
But the reason why 'a' knows that it should store a string is because it's inferring the data type from
the value. Because it knows that the value that you've given it, is a string.
So therefore I must be a variable that holds strings.
And similarly if I changed 'a' to hold a number, then it will be able to correctly infer that 'oh I'm actually
a variable that holds integers' And it's very clever about this because as soon as you change the value
to have a decimal point, then it knows 'oh actually now I'm a variable that can hold doubles.'
But what if you did this.
What if you create this variable and you don't give it a value at all?
You just say here's an empty box.
It's called a''.
And at some point later on down the line, you decide to give it a value let's say, 'a' equals 12
3.
Now what do we think is the data type of 'a' at this point?
Now at this point, 'a' is now dynamic. And I can prove that because I can now set 'a' to equal a string and
it won't complain about this at all.
And in fact I can even print my string 'a' into the console and everything will work just as you would
expect for a dynamically typed programming language.
So what's going on here?
Well, Dart also has a data type called dynamic. And what this means is that you can create a variable that
doesn't have to have a fixed data type.
So if I was creating a variable and I wanted it to be a fixed string data type, then I would write string.
If I wanted to create a variable that had an integer as the data type, then I would write 'int' in the beginning
instead of var.
Now I could also write dynamic as the key word.
And now my variable c is dynamic.
So if 'a' is only able to hold strings, then I can put the value 'hello' inside it. But I can't put the value
123.
This will generate an error. But if I had my variable created as a dynamic type, then I can put any sort
of data in it. I can put 123 and that will be just fine. It'll print out c without a problem
I can change c into a string if I wanted to.
And now when I print the string, hello
now gets printed.
This is how dynamic types work. And when we created the var without giving it a value, we essentially
created a dynamic type variable.
So when I click on 'a' you can see it's data type is dynamic. Because it didn't get a value, it couldn't
work out what data type it was meant to hold.
So it instead created a dynamic type.
The important thing here to take away is that if you want to use Dart for its type safety,
so ensuring that you don't mess up the data type and you don't accidentally put in the wrong thing that
you didn't mean to, then it's best to create your variables with the data type to start with.
So if we wanted to create a variable that holds a string, then we would write string a = hello or
whatever value you want.
If we wanted to create a variable that's an int, then we would write int b = 123 or whatever
maybe.
And if you're creating a variable that you don't yet have a value for, say the score of the game as soon
as the game starts, won't really have a value.
Well then in that case you can create an int and give it a name, but not give it a value.
But this way further down the line, c can never pulled a string.
It can't ever change its data type because when we created it, we already specified it.
So in terms of best practice, I recommend to avoid using var, and avoid using dynamic data types unless
you actually need it for a reason. Because it can lead to unexpected things happening in your code, and
it can lead to crashes down the line.
So in this lesson, we talked about data types and the primitive data types that Dart has access to.
And we also talked about how Dart can be a statically typed language because once you create a variable
that has a fixed type, then it won't accept values that are of different data types.
But we also saw how Dart can have this dynamic typing, which makes it a lot more similar to dynamically
type languages such as JavaScript. Now the official Dart documentation says that Dart 2,
so the latest version of Dart, is a statically typed language. Which means that we can't switch around
the data type of a variable once we've created it. But it does say that you can use this dynamic keyword
if you want to be able to change the data type.
But it does of course remove the benefits of a type-safe language. So I recommend to avoid using the
keyword var, and avoid using dynamic types when you're working with Dart, in order for your code to be less
error prone.
Now at this stage, it might not affect us very much. But later, on as our code gets more complex and does
a lot more things, it becomes a lot easier to work with Dart as a statically type language.
But if you come from JavaScript or another language that has dynamic typing, just be aware that you can
also do the same with Dart. Now in the next lesson,
we're going to head back to making our apps and we're going to learn about stateful and stateless
widgets in Flutter.