Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create callback function in dart flutter?

Tags:

flutter

dart

I have this method with onTap parameter

myFunc({onTap}){
   return onTap;
}

then, I need to use it like this

myFunc(
   onTap: print('lorem ipsum');
)

How I can make it correctly? thanks

like image 901
Ashtav Avatar asked Jul 31 '19 03:07

Ashtav


People also ask

How do you implement callbacks in flutter?

Implementation Step 1: Add Firebase to Flutter Add Firebase to your Flutter app | Firebase Follow this guide to add Firebase products to a Flutter app. Firebase supports frameworks like Flutter on a best-effort… firebase.google.com Step 2: Add the dependencies Add dependencies to pubspec. yaml file.

What is callback function in Dart?

Callback is basically a function or a method that we pass as an argument into another function or a method to perform an action. In the simplest words, we can say that Callback or VoidCallback are used while sending data from one method to another and vice-versa.

What is VoidCallback in flutter?

VoidCallback = void Function() Signature of callbacks that have no arguments and return no data.

How do you write a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.


1 Answers

You can do like below. Note that you can specify parameter or avoid and I have added Function(You can use ValueChange, Voidcallback)

myFunc({Function onTap}){
   onTap();
}

//invoke
myFunc(onTap: () {});

If you want to pass arguments:

myFunc({Function onTap}){
   onTap("hello");
}

//invoke
myFunc(onTap: (String text) {});
like image 169
Blasanka Avatar answered Oct 31 '22 07:10

Blasanka