So now that you've learned about all three types of functions in Dart, can you try and solve this problem
that we have here and make our error go away?
So if you haven't fixed it yet,
pause the video and try and complete the challenge. All right.
So at the moment, we've got the arrow that says the expression here is a type of void and therefore cannot
be used.
And the reason is because this function buildKey doesn't return anything. It outputs void which means
it outputs nothing.
So in order to change this, we have to change the output type and also use that return keyword to specify
what needs to come out of this function.
So first let's change the void to the return type, and what is going to come out of this function?
Well, essentially we want to have a whole bunch of expanded widgets in each of these places where we're
calling buildKey. So we can simply specify that data type of our expanded widget, so we can write expanded
write here.
And now this function, once it gets called, should return something that has a data type of expanded.
And that's exactly what we're going to do here.
We're going to write return.
And the thing that we're going to be returning is this expanded widget.
So now, each time we call buildKey, it will go and find where this function was created,
and it will look inside the curly braces of this function to see what it needs to do.
And in this case, it simply just needs to return an expanded widget which contains a flat button which
has a red color
and when it gets pressed, it plays note1.
So let's save this and run this in our simulator. And you can see we have a whole bunch of red buttons.
And very faintly, you can see where they separate from each other.
So this works but this doesn't look great, right?
And we've also lost all that functionality that we added previously. But we've got our function now so
we can start providing an input in between these parentheses to tell our function to build a different
expanded widget each time.
So for the first one, we might want to build one that has a color of red, and it plays sound 1. But for
the second one, we probably want it to have a different color and play a different sound.
So we have our buildKey and in between the parentheses, we can specify the inputs which has a color
property, so we could write colors.red.
And it also has a sound number property,
and this one is going to take a integer.
So in this case, it's going to be the sound number 1 that's going to be played.
So now the challenge is can you change the code so that we're still using this buildKey function to
build each of our Xylophone keys and update the rest of this code so that we get different colors and
different sounds for each of the keys that we're building?
Pause the video and try to complete this challenge.
All right.
So first things first, let's try and make this code work first.
Now in order to do this, our build key function needs to be able to accept some arguments or some inputs.
And here it says well, this thing called color and this thing called sound number isn't defined.
So let's go ahead and define it right here. And remember that if we want to have named parameters, then
we need to enclose the inputs inside some curly braces, like this.
And the first input has a data type of color, and we decide to call that property just the word color,
but of course in lower case.
And then the second property was called sound number, and it has a data type of integer. So now, this line
no longer gives us an error here
because it's now valid code. We're providing a color and we're providing a sound
number. But we now need to use what we get through the input inside this buildKey function.
So instead of using color.red, we're going to use the color that we get from our input. And instead
of playing the sound, that's sound 1, then we're going to play the sound that is the sound
number,
whatever it may be that's passed in as the input. For this key, the color input is red,
so that will go in here and the sound number is 1,
so that will go in here. And we're going to update all of these other build keys so that we get a different
number and a different sound.
I'm just gonna copy that and paste it a few times. And let's just make sure that we've got 1, 2, 3
4, 5, 6 and 7. And now I'm gonna change all the colors.
All right.
So now if we hit save, we should see all our buttons change to the corresponding color that we're using
to build each key.
Now all we need is to change each of these sound numbers so that when they get pressed, they play different
sounds.
All right.
So now we've dramatically reduced the amount of code that's required to build our Xylophone app by building
an expanded widget
each time we need one.
And because we have these inputs, we're able to customize them for each key so that they each have a
different color and they eat play a different sound.
And this is all thanks to the abilities of functions. In the next lesson,
I want to talk about one last thing that's related to functions and it's this syntax right up here.
Now previously, we kind of glossed over this until we started covering functions. But in the next lesson,
I'm going to talk about this in more depth.
So all of that and more, I'll see you on the next lesson.