Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with calling function using onPressed in Flutter

Tags:

flutter

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

Error when onPressed

like image 699
Jimmy Vailer Avatar asked Mar 26 '19 22:03

Jimmy Vailer


2 Answers

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(),
like image 191
diegoveloper Avatar answered Nov 08 '22 05:11

diegoveloper


You need to check and remove contanier(widget) const like this

(X)

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",
                ),
              )

(O)

child: TextFormField(
                decoration:InputDecoration(
                 ...
              )
like image 6
Kai Avatar answered Nov 08 '22 06:11

Kai