Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 2: Learn Anonymous Functions & Closures

13. Challenge: Write an Anonymous Method

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: 12. Map & Filter Collections Next episode: 14. Create a Closure

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: 13. Challenge: Write an Anonymous Method

By now, you should feel comfortable writing anonymous methods so why not practice with a challenge? In your challenge, I want you to create a list of scores of forty three, eighty six, ninety nine, and three. Put those scores in a list.

Then I want you to filter the list using a function called firstWhere. FirstWhere will return the first item per a filtering criteria. It’s pretty much the same as Where except instead of returning a collection, it just returns a single item.

I want you to get the first number that is even. To get an even number, you use the modulo operator. The modulo operator gets the remainder of a division operation. So if you have a number like ten, then ten modulo 2 will equal zero since if you divide ten by two, you get five with nothing left over. However, if you divide eleven by two, you’d have one left over. So eleven modulo two equals one. Don’t worry if you are confused, I put the code snippet on the challenge slide.

That said, give yourself bonus points for condensing the anonymous function into one line. Pause the video and give it a try.

How’d that go for you? Hopefully you didn’t find the modulo code too distracting. It’s a great tool to use when iterating over a collection to determine even and odd rows.

Okay, lets get started by creating a list.

  var numbers = [43, 86, 99, 3];

Now we are going to get the first even number. We’ll create a variable to hold the number, calling firstWhere on the numbers collection.

var even = numbers.firstWhere();

This method takes in a function. First, it passes in the current item in the collection

var even = numbers.firstWhere((item) {

});

At which point, we check to see if the number is even or odd.

var even = numbers.firstWhere((item) {
    if (item % 2 == 0) {
        return true;
    } else {
        return false;
    }
});

Now let’s print out the number.

print(even);

Now run the program. And we get our first even number which is eighty six. Okay, now let’s tighten our anonymous function. It’s syntactically correct, but it’s verbose. We can do the same thing with less code.

First, we don’t need to explicitly return from the if check. We can just return the result of the expression.

var even = numbers.firstWhere((item) {
    return (item % 2 == 0);
});

Better still, we can use arrow notation too tighten it even further.

var even = numbers.firstWhere((item) => item % 2 == 0);

Now we have a compact function that is clear without a lot of code.