🎥Intro

You may have been using Lexical Closures without actually realizing it. Functions defined inside another function (parent) often have unrestricted access to the local variables of the parent function. Some languages allow this and Dart is one of them. While this feature certainly seems useful, how is it any different from declaring a global variable or maybe even an instance variable( if they’re member functions of a class) for both the functions? Is there any specific use case? We’ll answer those questions with an example. Keep reading.

Now let’s say we have an app that displays a DataTable Widget in Flutter like this one here.

Image for post

A Flutter app that displays a DataTable Widget.

As you can see,

the program above basically prints a list of elements in a table. Each cell has an onTap property that takes in a function(an anonymous function in this case) of type VoidCallBack.

#flutter #dart #lexical-scope

Understanding Lexical Closures in Dart(Flutter)
5.95 GEEK