35

I have a simple form with a button to calculate the form. I figure it's better to hit the button to start the action of calculating and pass the variables to the dumb function than to make the function aware of text fields it shouldn't need to know about. Can I do that or does my calculate function need to access my TextFields?

         new Container(
            color: Colors.grey.shade300,
            padding: new EdgeInsets.all(15.0),
            child: new Column(
              children: <Widget>[
                new TextField(
                  controller: _ageController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.person_outline), labelText: "Age"),
                ),
                new TextField(
                  controller: _heightController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.insert_chart),
                      labelText: "Height (in feet)"),
                ),
                new TextField(
                  controller: _weightController,
                  decoration: new InputDecoration(
                      icon: new Icon(Icons.line_weight),
                      labelText: "Weight (in lbs)"),
                ),
                new Padding(padding: new EdgeInsets.only(top: 20.0)),
                new RaisedButton(
                  onPressed: calculate(1, 2),
                  color: Colors.pinkAccent,
                  child: new Text(
                    "Calculate",
                    style: new TextStyle(color: Colors.white),
                  ),
                )
              ],
            ),
          ),

        ],
      ),
    );
  }

  void calculate(num1, num2) {

  }
}
ThinkDigital
  • 3,189
  • 4
  • 26
  • 34

6 Answers6

74

e.g.

    int  result = 0;

    void calculate(num1, num2) {
     setState(() {
         result = num1 + num2;
     });
    }


    new RaisedButton(
          onPressed: () => calculate(1, 100),
          ...
    ),
    new Text("$result")
kevinbrink
  • 1,265
  • 8
  • 14
  • 7
    So actually we're not passing a function itself (recall that everything is an object in Dart, even a function) to `onPressed` property, we create an anonymous function which calls our function. The same as `onPressed: () { calculate(1,100);}` – Kirill Karmazin Aug 09 '19 at 18:49
  • Is this the only way to do it ? or Is there any other way to doing it ? – K Pradeep Kumar Reddy Aug 17 '20 at 07:17
11

Do you remember the very first line in flutter which is

void main() => runApp(MyApp());

Where => represents one line functions or methods which is key while calling functions with arguments.

Ex:

void display() {

}

we can call the above function by

IconButton(icon: Icon(Icons.add), onPressed: display)

here we can not include () with display

But, when there is one or more than one arguments to be passed then it goes like below

void addition(int value1, int value2) {
    value1 + value2
}

IconButton(icon: Icon(Icons.add), onPressed: ()=>addition(2, 4))
PradeepKN
  • 617
  • 7
  • 10
7

Just found the answer. No. According to the onPressed property of the RaisedButton class, the onPressed property has a type of VoidCallback, which return void and according to the docs VoidCallBack is the Signature of callbacks that have no arguments and return no data.

Edit: Thanks guys. To solve this, you use an anonymous function, like so

(){your code here}

ThinkDigital
  • 3,189
  • 4
  • 26
  • 34
6

Shorthand function designed in such a way that it is allowed to return function with type void as mentioned in this article --> So why is returning everything allowed through a shorthand function[()=>] even if the return type is void?

So that you have two approaches in your disposal:

use

onPressed: () => calculate(1, 2),

or

onPressed: (){calculate(1, 2);},
SAndriy
  • 670
  • 2
  • 15
  • 25
3

Use

onPressed: (){calculate(1, 2);},
Shyju M
  • 9,387
  • 4
  • 43
  • 48
0

I've fixed mine like this in my code:

i had to pass a void function with params to an onPressed event.

onPressed: delete(param1); //error
onPressed:(){ delete(param1);} //error
onPressed:(randomargument){ delete(param1);} //good to go
TCC
  • 67
  • 1
  • 6