US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

How to Pass a Function With Parameters to a VoidCallback In Flutter?

How to Pass a Function With Parameters to a VoidCallback In Flutter

How to Pass a Function With Parameters to a VoidCallback In Flutter?

Earlier we have been through articles on a flutter like a what is the difference between the const and final keywords in dart so in this article we will go through how to pass a function with parameters to a VoidCallback in flutter.

In this blog post, we’ll explore how to pass a function with parameters to a VoidCallback in Flutter. As a Flutter developer, you often encounter scenarios where you need to pass a function along with its parameters to an event handler like a button’s onPressed callback. We’ll guide you through different techniques and strategies for achieving this, including using anonymous functions, closures, and higher-order functions. You’ll learn how to pass parameters dynamically and ensure proper data flow between functions.

How to Pass a Function With Parameters to a VoidCallback In Flutter?

The declaration of VoidCallback is

typedef void VoidCallback();

That is the type of functions that can be called with zero arguments and which does not return a useful value. That does not seem to be what you want.

It’s not entirely clear what you do want since the program isn’t syntactically valid, but would this work for you:

class MyClass { 
  static doSomething(int i) { /* ... */ }
  MyOtherClass myOtherClass = new MyOtherClass(doSomething);
}
class MyOtherClass {
  final void Function(int) callback;
  MyOtherClass(this.callback);
  void callCallaback() { callback(5); }
}

Here we define the type of the field to be the type of functions that can be called with one integer argument and which returns no useful value. The do something method has that type, so it can be assigned to the callback.

You could also use a typedef to name the function:

typedef Int2VoidFunc = void Function(int);
// or: typedef void Int2VoidFunc(int arg);
class MyOtherClass {
  final Int2VoidFunc callback;
  MyOtherClass(this.callback);
  void callCallaback() { callback(5); }
}

The effect is exactly the same, it just allows you to use a shorter name for the function type, but that only really makes sense if you use it a lot.

create your own callback instead

typedef void MyCallback(int foo);

class MyClass {
  void doSomething(int i){

  }

  MyOtherClass myOtherClass = new MyOtherClass(doSomething);
}


class MyOtherClass {
  final MyCallback callback;

  MyOtherClass(this.callback);

}

Example in UI case. You may need to create a widget and pass the click function.

1.Create a widget with function as a parameter in the constructor.

Container _cardButton({
  Function onClickAction,
}) {
  return Container(
    width: 340,
    height: 90,
    child: InkWell(
      splashColor: Colors.blue.withAlpha(30),
      onTap: () {
        onClickAction();
      },
      child: Card(
        elevation: 5,
        child: somechild,
      ),
    ),
  );

2. Implement widget to the three views and pass function like this

_cardButton(
    onClickAction: () => {debugPrint("CLICKED")},
),

So now, Just replace VoidCallback with Function(int)

class MyClass {
  void doSomething(int i){

  }

  MyOtherClass myOtherClass = new MyOtherClass(doSomething);
}


class MyOtherClass {
 //OP code (does not work): final VoidCallback callback(int); 
 final Function(int) callback;

  MyOtherClass(this.callback);

  callback(5);
}
  • Create typedef first in any constant class:
typedef StringVoidFunc = void Function(String);
  • Pass from calling function class one
    showAlertDialog(doSomething);
  • Call back handle Function
    void doSomething(String i){
    Navigator.pop(context);
    setState(() {
    
    });
    }
  • From where you want to fire call back from Class Two
       showAlertDialog(StringVoidFunc callback) {
         callback("delete");
    }

Conclusion:

So, We hope you have learned from this post:) Keep Learning!!!

So in this article, we have been through how to pass a function with parameters to a VoidCallback in the flutter…

Do not forget to drop your valuable suggestions/feedback. We would love to assist you.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Post a Comment