简体   繁体   中英

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

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?

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

testFunction(){
    myPlacement.requestContent;
}

use bracket for the testFunction

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM