Programming in Dart: Fundamentals · Challenge: Anonymous Functions and Closures | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4921688-programming-in-dart-fundamentals/lessons/23

The video for Lesson 23 is the same Video as lesson 22 instead of being the Challenge: Anonymous Functions and Closures video. Lesson 24 Arrow functions is correct (as are lessons 1 through 22).

Thanks @mrpaulb! We’ll fix this right away.

1 Like

Thanks again @mrpaulb! This is now fixed.

1 Like

In the video, the repeatTask function take other Function (task) as parameters, and then inside the repeatTask body, invoke the task and pass the input and result as argument, the question is how do we know the ‘task’ will accept how many parameters or make sure it just take only 1 parameter as we just define the type as Function?

You can use function type syntax for parameters.

To provide a quick example using the challenge solution as the base (video-pd1-materials/main.dart at versions/1.0 · raywenderlich/video-pd1-materials · GitHub)

On line 48, replace the following:
int repeatTask(int times, int input, Function task) {

With:
int repeatTask(int times, int input, Function(int) task) {

However, the function type syntax is not necessary in this case, as Dart’s static analyzer can infer the type from the function body. If you paste the code into DartPad and hover over the variable names, for example result, you will see in the documentation section it will state: int result local variable

For alternative examples of the syntax, see the following stackoverflow topic:

1 Like