PopupMenuButton sending Buildcontext to handleclick function

142 Views Asked by At

I am pretty new to flutter and I found the code for my PopupMenuButton online and modified it slightly. I want the logout option to run Navigator.pop(context), but I don't understand how to get it get it there. Code:

PopupMenuButton<String>(
      onSelected: handleClick,
      itemBuilder: (context) {
      return {'Logout', 'Settings'}.map((String choice) {
        return PopupMenuItem<String>(
           value: choice,
        child: Text(choice)
      );
    }).toList();
  }
),

And I want to send it in here:

void handleClick(String value, BuildContext context) {
  switch (value) {
    case 'Logout':
      storage.deleteAll();
      //Navigator.pop();

      break;
    case 'Settings':
      break;
  }
}

I also tried making Buildcontext a global final late variable, but it seemed to cause a lot of problems. To be honest, I don't understand how the PopupMenuButton works in regards to calling the funtion, thus me not knowing how to pass another argument Thanks for the help

1

There are 1 best solutions below

2
On

Write onSelected like this instead:

onSelected: (value) {
  handleClick(value, context);
},