Cross-Platform Mobile Apps with Flutter

Collections & Null Safety

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Cross-Platform Mobile Apps with Flutter

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

The "Collections & Null Safety" 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 the different types of collections in Dart. Lists are typed arrays. A set is an array-like structure that cannot contain duplicate values and a map is similar to a JavaScript Object. Dart's null safety feature is also explained in this segment.

Preview
Close

Transcript from the "Collections & Null Safety" Lesson

[00:00:00]
>> So talking about collections, I think that this will be straightforward if you're a JavaScript developer. There are many collections within the Dart framework. We're going to see the most important ones, the ones that we will use later in Flutter. So we have list that are kind of arrays.

[00:00:19]
The type, we declare the type with capital L, List. And within angle brackets, what are we expecting inside the list? So list of a string will only accept the strings within that array. And we initialize or we have a literal, a list with JSON syntax. So we open square brackets and we put values inside, comma separated, as we are used to in JSON, okay?

[00:00:47]
So that's pretty straightforward. We also have sets. What's a set? A set, this is new, so it's different from the others you have. A set actually use curly braces for the list instead of square brackets. The difference, the set will not accept repetitions and there is no actual order inside.

[00:01:11]
That's why here it's giving me some kind of a warning. It's not an error. So you will see that errors will appear in red, and also the file will now be in red. Let me click somewhere else. So you can see that that file is in red, okay?

[00:01:26]
That means that's an error. And when you have warnings, and also you will see the red, they are in the tab. When you have warnings, you won't get that red. It's just a warning saying, hey, it seems like something might be wrong here. In this case, I have c twice.

[00:01:42]
It's telling me, hey, that's not gonna work. I mean, it will compile, but after this, the length of the strings will be three, not four, because a set is not accepting duplicates, okay? And then we have, Maps that is similar to a dictionary or an object in JSON.

[00:02:10]
When you use JSON syntax, that is actually pretty similar. You have key value. The difference that we have on the type, the type is called map of a string integer is that in Dart, the keys doesn't need to be strings. So as a key, you can put a number, you can put a Boolean, you can put another object, wherever you want as a key.

[00:02:35]
So as a key and as a value, you can pick the type. So if you have a JSON, so let's say that you have a JSON object that has a name, Jane, and you have age, and the age is a number. So what's the type now? Because for the key, let me increase the font size a little bit, for the key, we have strings and a string, right?

[00:02:58]
So let me put this like this. By the way, every time you save, by default, the plugin is changing how you're writing this. So look at this. I'm doing this and that, I'm saving, Cmd+S, Ctrl+S, and it's changing this. What if I use a trailing comma? If you use a trailing comma and I save, it's now designing this or formatting my code in multiline.

[00:03:29]
So Dart accepts trailing commas. When you use trailing commas, the formatting guideline is to use several lines, okay? So you can add a trailing comma anywhere, and that will actually make that, okay? So if you add the trailing comma on the array after Denmark, I add a trailing comma and I save, it's changing the format of the code.

[00:03:57]
Of course, that's an IDE definition and you can change that if you don't want that, but the Flutter and the Dart default guideline looks like this. So what's the type of the JSON variable? The type is actually a map. The key is a string, so you can see, I have two strings here.

[00:04:14]
But what about the values? The values are not of the same kind. So in that case, the implicit type will take, if I hover over JSON, object. Object, it's similar to dynamic, but dynamic accepts also something that might not be an object. You say, what, you mentioned that everything is an object.

[00:04:38]
Well, there are a couple of exceptions, for example, null. The null value is not an object. Okay, so there are a couple of exception, the other idea. So there are difference between dynamic and object, but at this point, I think that it's okay. I mean, we don't need to get too deep into the low level parts of Dart.

[00:04:59]
We wanna write an app at this point, okay? So that's for collections. Null safety, you were waiting for this. So null safety works like this. First, null safety is not by default in Dart. It's by default in Flutter. Because when you create the project in Flutter, it automatically prepopulates the Dart configuration with null safety, okay?

[00:05:23]
But null safety was added actually not so far ago, in 2021 was actually stable. So the idea is that when you create the variable like this one, String myName, if I don't set the value, it doesn't compile. Because it says, hey, you need to initialize this. And you say, well, can we initialize with null?

[00:05:46]
No, you cannot do that. A value of type null can't be assigned to a variable of type string. If you say that your variable will contain a string, it must contain a string, unless you use something like this. You can apply the late prefix. Late means I don't have the value now for the string.

[00:06:09]
I will set it later, okay? And then you are responsible of setting a value before using the variable. If you try to use the variable without a value, it will crash your app or it will make an exception on the web, okay? Or you can define the semantic null level type.

[00:06:34]
And in that case, you use a question mark after the type, that one, okay, a question mark after the type. That says that it's a null level string, so it can be a string or it can be null. I can explicitly put a null or not. I don't need to explicitly say equals to null.

[00:06:52]
But now, it's like a Java or a C# default object. It can accept null, but I need to explicitly say so in the type declaration, okay, that I accept null semantically. But because we haven't a null level type now, there are some issues. For example, if I try to execute this code, the length of myLastname, it doesn't work.

[00:07:21]
I'm seeing an error, a compilation error. You cannot get the length of a null level string. Why, does anyone know why?
>> Because it might be null.
>> Because it might be null. And if it's null, we cannot access a length or a method or a property. We will get the famous null pointer exception or null reference exception.

[00:07:43]
It depends on the platform. And we don't want that. So that's why the platform, the language, is offering us new ways to solve the problem. So we have actually two ways, the null manual override that actually it's exclamation mark after the null level variable. In that case, we are assuming it's not null.

[00:08:13]
I'm saying to the compiler, compiler, I know this variable might be null. But I also know because I'm the developer of this algorithm that it's not null, so go ahead and get the length. Of course, if it's null, you will have a problem, a runtime error, okay? So you need to be very careful or you need to be responsible when you are using this operator.

[00:08:35]
And also we have the safe call operator that is actually question mark dot. In this case, if it's null, it's not going to execute length. So it will ignore what's after the dot, okay? But because it will ignore it, question for you, what do you think the type of length is?

[00:08:59]
Is it an integer? Is it int? Int question mark, because if there is a last name, we will have an integer. But if we don't have a last name, we don't have an integer. So it's an optional integer, okay, makes sense? So that's null safety. If you're coming from JavaScript where you have never seen this, I mean, it takes a while to get used to that.

[00:09:25]
Let me say that fortunately, most of the Flutter framework is not using this heavily. So we are going to use this only in our own business project and our own classes. But if you're working with the UI, working with the Flutter framework, we don't use optionals a lot.

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