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)
),
],
),
);
}
Flutter Button on Pressed To execute a function when button is pressed, use onPressed() property of the button.
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.
Create a bool variable which will be true when the button is pressed, (hence, initial value is set to false ).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With