Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 2: Learn Anonymous Functions & Closures

17. Challenge: Write a Generic Function

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 16. Learn Generic Functions Next episode: 18. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 17. Challenge: Write a Generic Function

Now it’s time for the last and final challenge of this course. You just watched two episodes on generics. Now it’s time to put your knowledge to the test.

Here’s what I want you to do - Imagine you are a teacher, and you have a list of grades for each student. In order to calculate the average grade for the entire class, you put each list in one big list. But you need to process it as one large flat list. I want you to write a generic function that will take a list of lists and condense it into a single list that you will return back to the user.

Please refer to the challenge slide for some sample data and then, give it a try. Pause the video now and try it out.

How did that work out for you? Hopefully, it wasn’t too difficult. Understanding generics is crucial for you being successful with the Dart language so make sure to keep practicing and reading on the subject.

Okay, we’re going to start by defining a very lists.

var scores = [
    [96, 46, 87],
    [87, 78, 80],
    [90, 86, 95],
];

var interests = [
    ['Video Games', 'Reading', 'Soccer'],
    ['Gymnastics', 'Step Dancing', 'Softball'],
    ['Horseback Riding', 'Hockey', 'Writing']
];

Now we defined two different types of lists. One list is the scores of the students and the other list contains all the interests. Now lets define our generic function.

This will return a List.

List<T>

Remember, the T stands for type. Let’s give it a name.

List<T> flattenList

So we have a function that returns a single list of type T. We now need to define our type.

List<T> flattenList<T>

Next up, we’ll pass in a list of lists. This is where the syntax gets really weird.

List<T> flattenList<T>(List<List<T>> list) {

}

This just takes in a list of lists. That’s the problem with generics. They can look confusing at a glance. Okay, now let’s define our flat list that will hold all the values.

var flatList = <T>[];

Now for the flatten operation. There are lots of way to do this, but for us, we’re going to loop through each list and then put each item in the list.

for (var item in list) {
    for (var subItem in item) {

    }
}

Then we’ll add each item to our flatList.

flatList.add(subItem);

And let’s return our flatList at the end of the function.

return flatList;

Okay, we’ve defined our function. This is a good case for documentation. I might be able to understand the meaning with the name, but you never know. Here I’ll just add a simple comment above the function.

// Takes in a list of lists and extracts all items into a single list

Now lets flatten our scores and interests.

var flatScores = flattenList(scores);
var flatInterests = flattenList(interests);

Last but not least, we’ll print out the results.

print(flatScores);
print(flatInterests);

Now we’ll run the program. I have an error with a missing parenthesis. I fix it and run again. And we flatten our list. Awesome. Again, generics can be confusing at first. Just be patient with yourself and know that understanding will come in time.