mirkancal
mirkancal

Reputation: 5365

How to pass data to PopupMenuButton widget's onSelected method?

I'm trying to delete my Cards via PopupMenuButton.

Day Cards

But I can't pass any info about current card to its onSelected method, it takes String and returns void.

onSelected: (String value) { print('Selected: $value'); },

Upvotes: 0

Views: 3713

Answers (3)

Farman Ameer
Farman Ameer

Reputation: 1472

PopupMenuButton<String>(
          itemBuilder: (context) => [
            PopupMenuItem(
              child: Text("Edit"),
              value: "edit",
            ),
            PopupMenuItem(
              child: Text("Delete"),
              value: "del",
            )
          ],
          onSelected: (String val) {
            print(val);               
          },
        )

Upvotes: 0

jumair ahmad
jumair ahmad

Reputation: 21

If someone is facing the same issue still,

   In PopupMenuItem you have to specify the value parameter so when a popup item  
    is clicked it sends the corresponding value to onSelected method 

    PopupMenuItem(
    value: value,
    child: Text(value));
    }

Upvotes: 0

Keerti Purswani
Keerti Purswani

Reputation: 5381

Just call another function from there and pass your value -

onSelected: (Choice choice) {
          callMethod('some data I am passing');
        },

and then call your method like this-

callMethod(String data) {
print(data);}

Upvotes: 6

Related Questions