Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: button onpress action function continuously calling

Tags:

flutter

dart

When I add onPress function to the button, loadMore function is called when the app starts. And It continuously increate the page number. I couldn't find what is wrong.

Widget header(){
    return new Container(      
      color: Colors.blue[900],
      height: 40.0,
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          new IconButton(
            icon: new Icon(
              Icons.arrow_back,
            ),
            onPressed: loadMore(page: page = page + 1)
          ),
        ],
      ),
    );
  }
like image 458
serdar aylanc Avatar asked May 04 '18 12:05

serdar aylanc


People also ask

How do you call a function Onpress in flutter?

Flutter Button on Pressed To execute a function when button is pressed, use onPressed() property of the button.

How do you stop double clicking in flutter?

Wrap code to TapDebouncer onTap) { return RaisedButton( color: Colors. blue, disabledColor: Colors. grey, onPressed: onTap, // It is just onTap from builder callback child: const Text('Short'), ); }, ), //... Debouncer will disable the RaisedButton by setting onPressed to null while onTap is being executed.

How do you make a clickable false in flutter?

Create a bool variable which will be true when the button is pressed, (hence, initial value is set to false ).


1 Answers

You have a function invocation, but you should pass a function reference

onPressed: () => loadMore(page: page = page + 1)

If loadMore didn't have parameters (actually when it has the same parameters the caller uses to invoke the method), you could use

onPressed: loadMore

to pass the function reference without creating an closure.

With your code

onPressed: loadMore(page: page = page + 1)

loadMore(...) will be called every time Flutter runs build()

like image 190
Günter Zöchbauer Avatar answered Nov 15 '22 07:11

Günter Zöchbauer