Dividing Lists with SubList

A free video tutorial from Stephen Grider
Engineering Architect
Rating: 4.7 out of 5Instructor rating
36 courses
1,372,053 students
Dividing Lists with SubList

Learn more from the full course

Dart and Flutter: The Complete Developer's Guide

Everything you need to know for building mobile apps with Flutter and Dart, including RxDart and Animations!

31:11:50 of on-demand video • Updated February 2024

Build beautiful multi-screen apps with Flutter
Understand the different approaches for loading and maintaining data in Flutter apps
Create and publish open source projects using Dart
Learn how to build offline-enabled apps with incredibly efficient data loading
Comprehend the best method for reading the incredible amount of Flutter documentation
Store long-term data on a user's physical device using SQLite
Master streams and understand reactive programming to create Flutter apps
English [Auto]
In the last section, we had a deep discussion on how this sub list method right here actually works. So when it says that it returns a new list containing the objects from a start index to an end index, we now understand that it's not actually kind of copying those objects. It's not removing them from the original list. We are just copying references to those objects and they never get removed from the original list. So with that in mind, we're now going to flip back over to Dart Pad and we're going to implement our deal method. We're going to figure out how to actually modify our original underlying deck when we create this hand. Because as we just discussed a moment ago, if I deal you some number of cards out of my deck, like, say, cards zero one and two, I definitely should not retain access to those cards. When I deal them to you. That's it. They're no longer inside of my hand of cards or my deck of cards. I no longer have access to them. Okay. So I'm going to flip back over to dart pad. I'm going to find my deck class. Here it is right here. I'm going to go down to the bottom of that class again. And while still inside of the class body, I'm going to create a new method called deal. Now, whenever we call this function right here, we probably need to know how many cards we're trying to deal out to a user. For example, if we are playing a game of poker, I might need to deal you five cards. But if we're playing a game of blackjack, I'll probably only deal you like what, two cards or whatever it's supposed to be. So as an argument to this deal function, I'm going to expect to receive an integer reflecting the hand size or how many cards I'm supposed to deal into your hand. Okay, so that looks good. Now the next thing we're going to do is we're going to take our cards list and we're going to call that sub list method on it. And we're actually going to call this sub list method on it two times. Okay. So let me give you a quick diagram of how this thing is going to work. We're going to call this sub list method two times. The first time you and I are going to pull out some number of cards that are going to be returned to whoever called this function. Then we're going to call the sub list method a second time. And in that second call, remember that we're not actually removing these cards per se. So let me copy paste them back up here to reflect reality. So in the first sub list call, we're going to create this second list down here or this kind of hand that we're going to return. And then in a second call to Sub List, we're going to pull off all the remaining cards. So like three, four, five over here, that's going to give us a new list. We're going to take that list and assign it over the current cards list. So in other words, we're going to call Sub List a second time. We're going to get a new list over here. We're going to throw this old list away and then we're going to say that the new cards list that belongs to our deck class is just these remaining three, four and five. So essentially, we're just going to call sub list twice and make use of these two different slices. One is going to go to whoever called the deal method and the other is going to go to the cards property. On our deck class. I think that once we write out the code for this, it'll make it a lot more sense. So down here inside of Deal, I'm going to first create my hand of cards or kind of the the deck or the list of cards that we're going to return from this function. So we'll say var hand is cards dot sub list, and I'm going to take all the cards from zero to hand size like so. So with this line of code right here, we are creating a new list and we are copying references over to that new list and we assign all that to hand. Then after that I'm going to call cards sub list again and we're going to take all the cards that are remaining inside this hand. Starting at hand size. So notice how right here we took everything from the start to hand size. Then on sublist right here, we're going to take everything that is remaining in the deck. So we'll say hand size on now for going all the way to the end of the list here or getting all the remaining cards. If you go back and look at the documentation here. You'll notice that around the oops, I accidentally clicked on that. I apologize. Let's stop now. Go back. Okay. So around that second argument. There we go around the second argument. We've got a set of square braces right there. So whenever you see those square braces around an argument list, that means that it is an optional argument. So in other words, if we do not pass in an end argument right here, subList is going to automatically go all the way to the very end of the list and take everything that is remaining inside there. So all we have to do is pass in sublist as the first argument right here, and then we do not have to pass in a second argument. So this line of code right here is going to take all the remaining cards inside of our cards list, and then we're going to take that brand new list that gets returned and we're going to overwrite our current list of cards with that one. And then finally. We will return the hand that we created. And that's pretty much it. Okay, so let's test this out. Now I'm going to go back up to my main method. I'm going to do a little bit of changing inside of our main method here. Let's just go back to where we had the deck. I'll start off by printing the deck. So we'll just kind of start off and say, Hey, what do we have here? I'll then call print on deck dot deal and I'll try to deal myself five cards. So now we're going to see two print statements. We'll see the original deck completely unmodified. We'll then see a hand of just five cards or a list of five cards by itself, and then we'll print out the deck again. So the second deck right here should have five fewer cards inside of it because we took five cards and we dealt them out to, I don't know, someone who's playing our game, presumably. All right. So let's give this a shot. I'll click run up here. Give it a second. Oh, it gets stuck all the darn time. Let's try this again. There we go. Much better. Okay, so right here, you'll see. Here is our original list of cards. That's all the cards that we're creating inside of our deck. Then right here, I see one, two, three, four and five cards that were created inside of our hand. And then we see right here, there's clearly I'm not going to count them all out, but clearly there are fewer cards inside of here. And actually, we can kind of make a quick comparison here. Notice how in the original list we had five diamonds cards. Those diamonds are now inside the hand and they are no longer inside of the list of cards that belong to our actual deck class. Okay, So I know that this sub list stuff was a little bit crazy, but remember the overall goal of what we were trying to communicate here is that whenever we create a list of records, the list does not actually truly store our records. The records exist inside of memory and the list just makes reference over to the records. Okay, so now that we got a better handle on that, let's continue in the next section.