Reputation: 493
I am trying to call a function upon an onPressed in Flutter.
I've tried
onPressed: (){
_showDialog;
},
and
onPressed: _showDialog,
and
onPressed: () => _showDialog,
This is my function.
void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Title"),
content: Text("Body"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
I keep getting "invalid constant value".
EDIT:
This is where I'm calling onPressed:
secondary: const IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
Upvotes: 17
Views: 21613
Reputation: 103381
UPDATE
Easy fix: remove const keyword
secondary: IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
Old Answer
You should try like these ways:
onPressed: (){
_showDialog();
},
or
onPressed: _showDialog,
or
onPressed: () => _showDialog(),
Upvotes: 48
Reputation: 186
You need to check and remove contanier(widget) const
like this
child: TextFormField(
decoration: **const** InputDecoration(
prefixIcon: Icon(Icons.person),
suffixIcon: IconButton(
icon: Icon(Icons.remove_red_eye),
onPressed: showPassword,
),
labelText: "Name *",
hintText: "Your Github account username",
),
)
child: TextFormField(
decoration:InputDecoration(
...
)
Upvotes: 6
Reputation: 1432
For some scenarios you can declare top-level or static function:
static void myHandler() {
_showDialog();
}
...
secondary: const IconButton(
icon: Icon(Icons.domain),
onPressed: myHandler,
),
Only in this case (top-level or static) myHandler can be treated as a constant and passed to the constant constructor.
Upvotes: -2
Reputation: 21
Remove const keyword
Your Code
secondary: const IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
Updated one
secondary: IconButton(
icon: Icon(Icons.domain),
onPressed: (){
_showDialog();
},
),
Upvotes: 0