Flutter onPressed - Difference between using a function or make a direct call

201 Views Asked by At

I am playing with Tapjoy's offerwall, I just don't know why this works:

ElevatedButton(
            child: Text("request content for Placement 001"),
            onPressed: myPlacement.requestContent,
          ),

And then this doesn't:

ElevatedButton(
            child: Text("request content for Placement 001"),
            onPressed: testFunction,
),

testFunction(){
    myPlacement.requestContent;
}

As you can see it's the same code but instead of calling directly I use a function...

requestContent returns a Future. This function internally makes a http request that I can see log in the console for the first option. The second one nothing happens..

Any ideas?

2

There are 2 best solutions below

0
Mohammad Nazmul Hasan On BEST ANSWER

This block of code will work

ElevatedButton(
            child: Text("request content for Placement 001"),
            onPressed: testFunction, ),

testFunction(){
    myPlacement.requestContent(); 
}

Use the bracket after myPlacement.requestContent inside the testFunction().

3
Yunus Kocatas On
ElevatedButton(
            child: Text("request content for Placement 001"),
            onPressed: testFunction(),
),

testFunction(){
    myPlacement.requestContent;
}

use bracket for the testFunction