Flutterexperts

Crafting Next-Gen Apps with Flutter Power
Working with Callback In Flutter

Hello Folks! We all are working in flutter and most of the time we all need to work with Callback and VoidCallback because we need to send data from one method to another and vice versa.

if you are a beginner then chances are you are struggling to handle the flow of data actually there are various ways to do that but Callback is the best suitable way to handle this. So lets first understand What is Callback ??

Callback:

If we go by definition, the Callback is a function or a method which we pass as an argument into another function or method and can perform an action when we require it.

Let’s understand why we use it

For Example, if you are working in any app and if you want any change in any value then what would you do?

Here you are in a dilemma that what you want to change either state() or a simple value/values. If you need to change states then you have various state-changing techniques but if you want to change simple values then you will use Callback.

Let’s Understand it by a small example I have made to make you understand,

here are two images below one is showing a text(“Want to change Text”) and a button by which you will navigate to next screen, In next screen, there is a text field and a button which navigate to the previous screen with the changed text you will enter in the text field.

Now see in the code

https://gist.github.com/shivanchalpandey/2ebbb6eadd0fd902406d0e0d53db01d7#file-homepage-dart

here in the above code snippet, a text is defined which is a default text and when we will receive a String value from callback it will be changed, then we will navigate to next class where we will perform Callback

https://gist.github.com/shivanchalpandey/3c3794086757e086e1517c10b516a83b#file-editingpage-dart

here in the EditingPage we have defined the Callback function

typedef VoidCallback = void Function();

which will return the value we will change to the previous page,

In the EditingPage class, Callback function has been initialized and below in code with the help of controller value has been assigned to it, when we will navigate to the previous page we will receive it here

here the value which has been sent by using Callback is being initialized to the “editableText “ variable and it will update the text.

So simple idea is that when you want to change any value you can use Callback and can handle the data flow easily.

So it was a small explanation to make you understand actually how it works and why and when you should use it.

Thanks for reading this article, if you find something you better know Please let me know, I will welcome it with full gratitude.


Feel free to connect with us
And read more articles from FlutterDevs.com

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, and Twitter for any flutter related queries.

Leave comment

Your email address will not be published. Required fields are marked with *.