Home > front end >  Flutter onPressed - Difference between using a function or make a direct call
Flutter onPressed - Difference between using a function or make a direct call

Time:01-08

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?

CodePudding user response:

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

CodePudding user response:

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

testFunction(){
    myPlacement.requestContent;
}

use bracket for the testFunction

  •  Tags:  
  • Related