Shorthand Function Syntax

Stephen Grider
A free video tutorial from Stephen Grider
Engineering Architect
4.6 instructor rating • 29 courses • 791,808 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 April 2021

  • 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 subclassed method right here actually works. So when it says that 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 d'arte 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 0 1 and 2 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 got to flip back over to d'arte pad 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 in to your hand OK. So it 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. We're actually in a call the subtlest method on it two times. Yes let me give you a quick diagram of how this thing's going to work. We're going to call this sublease 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 sublease method a second time and in that second call Remember that we're not actually removing these cards per se. Let me copy paste them back up here to reflect reality. So in the first published call we're going to create this second list down here where this kind of hand that we're going to return. And then in a second to call to sub list 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 a second time. We're going to get a new list over here. We're going to throw this all 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 sublease 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 card's 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 mean to first create my hand of cards or kind of the deck or the list of cards that we're going to return from this function. So we'll save var hand is Cards Dotts sub list and I'm going to take all the cards from zero to hand size. Like so. So at this line of code right here we are creating a new list and we are copying reference's over to that new list and we assign all that to hand. Then after that I'm going to call cards 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 subclassed 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 are 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 it. I apologize. Let's start 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 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 sub list 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 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 it will 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 too. I don't know someone who's playing our game presumably. All right so let's give this a shot a quick 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. OK. So right here you'll see here is our original list of cards. That's all the cards that we create inside of our deck. Then right here I see one two three four and five cards. They were carried inside of our hand. And then we see right here there is clearly 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 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 OK. So I know that this sublease 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.