You have used em' but you wanna know how it works.

A brief introduction to Closures in Dart ๐Ÿ’™

A thread ๐Ÿงต
So, how do you define the onTap callback for GestureDetector?

Something like this right ๐Ÿ‘‡๐Ÿป
So, what is the datatype of the data member onTap?

It is ----> void Function()

But, if you open the code for GestureDetector, onTap will be of type "GestureTapCallback", which inturn is an alias for : void Function()
But, how does this work in Dart ??

How are we storing a function in a variable like that ??

It's simple, we use the concept of Lexical Closures, aka Closures.
Closures are also known as first-class functions.
The basic idea is that a function is also a value that can be passed around to other functions as a parameter.

Closures can be saved to a variable and used as parameters for other functions.
The most important usage of closures is for defining callbacks.

๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ: onTap in GestureDetector, onPressed in ElevatedButton, and so on.
The easiest and most maintainable way to work with closures is with the typedef keyword.

We define an alias for a function as follows:
Note: Function is the superclass of all the function types in dart.

Now, we can use this alias in a function like below:
So, how do we call this function calculateArea ??

We use the code below :
Here, we are passing an anonymous function as a parameter , and this function returns the value 10.

Now, this function is used twice to return the value 10, inside the function calculateArea.

So, this is how closures work in Dart. But wait, now lets see GestureDetector.
If you open the definition of the onTap attribute, you will see something like the one below ๐Ÿ‘‡
If you open the definition for GestureTapCallBack? , you will see something like this ๐Ÿ‘‡๐Ÿป

Well, now it clears everything right. Now, we know how the onTap callback of GestureDetector works when we pass a callback.
But, how it internally works is out of the scope of this thread ๐Ÿ™ƒ

Hope you got a fair idea about Closures. If you liked it don't forget to like and retweet the first tweet !
Follow @GopinathanAswin for more threads like this. I will share more as I learn new things in the world of Dart and Flutter.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ

Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GopinathanAswin

20 Dec
This thread consists of all the ๐Ÿงตs i have written on various concepts in Dart and Flutter!

I will update this as i go onโ€ฆ

Anything for this wonderful community that has given me everything ๐Ÿ’™

#flutter #flutterdev
A ๐Ÿงต on using Futures the right way
Read 11 tweets
19 Dec
A ๐Ÿงต on my favourite 20 CLI commands for Dart and Flutter Developers!

#flutter #flutterdev
1. Create a Dart application
2. Run a Dart file
Read 23 tweets
18 Dec
Final vs Const

A ๐Ÿงตon the battle of Immutability โš”๏ธ

#flutter #flutterdev
Immutability means you cannot change the data stored in a variable during the entire life time of your app.

But why do we use immutable variables ?
Imagine you are working on an exam portal for your school. You are given the task of building an on-screen scientific calculator.

The most common symbol you use in maths is ฯ€ (Pi) which is equal to 3.1415.

This remains the same even after 100 years right ?
Read 19 tweets
17 Dec
Everything you need to know about Widget Lifecycle in Flutter

A thread ๐Ÿงต

#flutter #flutterdev
There are two types of Widgets in Flutter:

1. Stateless Widget : They do not change their state throughout the lifetime of your application. They are like a constant widget.

2. Stateful Widget: They are dynamic, they change their state based on the External Interactions.
A side note: Stateless widgets can change their state if some state management tools are used like Provider, Bloc, etc

But thatโ€™s out of the scope of this thread.

Letโ€™s continue ๐Ÿ‘‡๐Ÿป
Read 19 tweets
15 Dec
Mockito + Unit Testing in Flutter 101

This thread talks about Verify and Stubbing using Mockito๐Ÿงต

#flutter #flutterdev
If you are pretty new to Unit Testing, I suggest you read through my previous thread on Unit Testing:

So, what is Mockito and why do we need to use them while testing?

๐Ÿ‘‡๐Ÿป
Read 18 tweets
14 Dec
Unit Testing in Flutter 101

A mega thread ๐Ÿงต

#flutter #flutterdev
Unit Testing is the most important testing method, that is usually performed by the developer.

Unit Testing tests each individual modules (method, function or a class) for its proper functioning.
This indirectly means you have to maintain loose coupling in your application.

That is, each function or a method should be able to perform only one functionality.

This way it will be easier to test and even debug you codebase.
Read 19 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(