Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to callbacks in flutter?

Tags:

flutter

dart

class ExtractArgumentsScreen extends StatelessWidget {
  static const routeName = '/extractArguments';

  double price = 0;

  _changeprice(num){
      setState(){
        price+ = num;
      }
    }

  @override
  Widget build(BuildContext context) {
    final MealArguments args = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text('MyApp'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          SizedBox(height: 20,),

          ViewCard(args.combo,_changeprice()),
        ]));

This is the parent class. I passed the function _changePrice() to ViewCard so that I can change the value of the price later. This is giving me an

error "1 required argument(s) expected, but 0 found"

but I can't pass an argument to _changePrice as that defeats the whole purpose of using this callback. What I'm trying to implement is similar to what was suggested in Emit the data to parent Widget in Flutter.

The child class ViewCard passed this callback to another child called OneItem.

class OneItem extends StatefulWidget {
  OneItem(this.combo, this.index, this.changePrice);
  final Combo combo;
  final int index;
  final VoidCallback changePrice;

  @override
  State<StatefulWidget> createState() => OneItemState();
}

class OneItemState extends State<OneItem> {
  List<bool> Vals = new List();

  @override
  void initState() {
    super.initState();

    int count = widget.combo.items.length;

    Vals = List<bool>.generate(count, (_) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Align(
          child: Text(widget.combo.items[widget.index].item),
          alignment: Alignment(-1, 0),
        ),
        Align(
            child: Text(widget.combo.items[widget.index].price),
            alignment: Alignment(0.1, 0)),
        Align(
            child: Checkbox(
              value: Vals[widget.index],
              onChanged: (bool value) {
                setState(() {
                  Vals[widget.index] = value;
                  if(value == true){double childPrice = double.parse(widget.combo.items[widget.index].price); }
                  else{double val = double.parse(widget.combo.items[widget.index].price); 
                        double childPrice = -1*val;}
                  widget.changePrice(childPrice);                        
                  //widget.changePrice();
                });
              },
            ),
            alignment: Alignment(0.6, 0)),
      ],
    );
  }
}
like image 423
Hemabh Avatar asked Oct 16 '19 14:10

Hemabh


People also ask

How do you pass value in callback function in flutter?

You have the option to pass in a value with its variant Function(String val) which lets you receive the passed value in the callback function. As the name suggests, It lets you declare a function callback that does not pass in any value.

How do you pass parameters in flutter function?

How do you send a function as a parameter in Flutter? Step 1: Define a Dart method that takes a function parameter. Step 2: Create one or more functions to pass into that function parameter. Step 3: Pass the functions into the method.

How do you use callback in flutter?

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.

How do you pass future function as parameters in flutter?

You can do it like this, Future<String> _myFunction(Future<Response> Function() function) { ... }


2 Answers

You could declare a Function callback, like this:

final void Function(double price) changePrice;

Here another example to understand it better:

final bool Function(String id, double price) updateItem;

bool isPriceUpdated = widget.updateItem("item1", 7.5);
like image 74
Pablo Barrera Avatar answered Oct 13 '22 04:10

Pablo Barrera


You call widget.changePrice(childPrice); from OneItemState, so changePrice can't be VoidCallback

typedef IntCallback = Function(int num);

/* ... */

class OneItem extends StatefulWidget {
  OneItem(this.combo, this.index, this.changePrice);
  final Combo combo;
  final int index;
  final IntCallback changePrice;

  @override
  State<StatefulWidget> createState() => OneItemState();
}

/* ... */

ViewCard(args.combo, _changeprice) // function name without braces

I would do it like this

like image 26
Andrey Turkovsky Avatar answered Oct 13 '22 06:10

Andrey Turkovsky