serdar aylanc
serdar aylanc

Reputation: 1347

flutter: button onpress action function continuously calling

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)
          ),
        ],
      ),
    );
  }

Upvotes: 6

Views: 8790

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

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

Upvotes: 10

Related Questions