Shorthand Function Syntax

Stephen Grider
A free video tutorial from Stephen Grider
Engineering Architect
4.7 instructor rating • 30 courses • 970,380 students

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 January 2022

  • 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 sublist 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 Darte 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 hant, because as we just discussed a moment ago, if ideal, use 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. OK, so I'm gonna flip back over to Arpad. 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 deal 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. OK, 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 sublist method on it. We're actually going to call the sublist method on it two times. Let me give you a quick diagram of how this thing's going to work. We're going to call this sublist 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 sublist method a second time, and in that second call, remember that we're not actually removing these cards per say. So let me copy paste them back up here to reflect reality. So in the first sublist call, we're going to create the second list down here or this kind of hand that we're going to return. And then in a second call to Sublist, we're going to pull off all the remaining cards. So like three, four or 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 Sublist the 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 Sublist 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 debt 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 are going to first create my hand of cards or come to the deck or the list of cards that we're going to return from this function. So we'll save our hand is cards, dot sublist. 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 dot sublist 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 house, I accidentally clicked on it. I apologize. Let's stop now. Go back. OK, 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 the square braces around an argument list, that means that it is an optional argument. So in other words, if we do not pass in in any 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. OK, so let's test this out now and 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 deal and I'll try to deal myself five cards. So now we're going to see to 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 or click run up here. Give it a second. It gets stuck all the darn time. Let's try this again. There we go. Much better. OK, so right here, you'll see here is our original list of cards. That's all the cards that we're create 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 there. So we can kind of make a quick comparison here. Notice how in the original list we had five Dimond's 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. OK, so I know that this sublist 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. OK, so now that we got a better handle on that, let's continue in the next section.