In this video, learn how Kotlin handles nullability when it comes to collections. Learn about the difference between lists that can hold null values and those that cannot, as well as how you can set an entire list to null.
- [Narrator] Let's take a look at how Kotlin handles nullability when it comes to collections. For this, we're going to work with the nullability and collections file in the test folder. We're going to start with our first method, list cannot be null, and we'll come here on line nine and we're going to create a variable called flavors. And this is going to contain a list of strings, and we'll use the list of function in order to populate it. And our flavors are going to be vanilla, and then strawberry.
Okay, and on line 11 what we want to do is take our list and just go ahead and print it out. So we'll use for each and then print each element on it's own separate line. So let's go ahead and run this function, and make sure that we get all of our items. And there you have it, vanilla and strawberry. So let's try to change our list to be null. So we'll come here on line 10, and we'll take our flavors variable and set that equal to null.
Just add another line here. And notice that we get an error that we can't provide null to the non null type list. So this is going to bring us to our next method to see how we can achieve that. So let's go ahead and copy this code inside of our method. We can common it out for now. And then we'll come down to line 17 and we'll past it inside. But what we're going to do differently this time is here on line 17 where we have our list definition we're going to add a ? at the end.
Now notice that that error disappeared there on line 18. This is how we let the Kotlin compiler know that we're allowing this list to be nullable. And that's how we can set it to null. But that will require however, we have a null check here on line 20 before we can print out each item. So there you go, we have our list set to null, and now all of the errors have disappeared. But let's say we wanted to allow null elements into the actual list of flavors.
So let's copy the code we have here, and then attempt to add a null element. So we'll copy everything that we have here. And we'll move down to our next method and right at the end of line 25 we're going to add a null element. And then we'll delete line 26. So notice we get an error once again, this time it says there's a type mismatch. This is because we cannot set null items in this particular list.
Now in order to address this we have two options. We can just not allow null elements in the list, or we can update the type argument to allow them. So let's move to our final method so that we can see how that would work. We're going to copy the same code once again. Common it out so we can get rid of the errors for now. And let's paste down here in line 32. And what we're going to do is the same way that we added a ? to the end of our list definition.
We're going to add one right behind our string type, and this allows us to have that null element in the list. So hopefully it's clear how Kotlin can tell the difference between the collections that hold nullable and non-null elements.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections