Jimmy Vailer
Jimmy Vailer

Reputation: 493

Trouble with calling function using onPressed in 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

Upvotes: 17

Views: 21613

Answers (4)

diegoveloper
diegoveloper

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

Kai
Kai

Reputation: 186

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

Upvotes: 6

apc
apc

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

Ahmed_Abdirahman
Ahmed_Abdirahman

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

Related Questions