2

I have a PopupMenuButton that displays some PopupMenuItem<String>'s generated from a List<String>. Each item has a delete button, which removes the String from the list. The problem is that the popup menu doesn't get rebuilt after deleting an item, until it's closed and opened again.

It seems that no matter what I do, even using a GlobalKey and calling key.currentState.setState(), it doesn't cause the popup menu to be rebuilt until it's closed and opened again.

  GlobalKey _favoritesKey = new GlobalKey();

  PopupMenuButton<String>(
      key: _favoritesKey,
      icon: Icon(Icons.bookmark_border),
      itemBuilder: (context){
          List<PopupMenuItem<String>> result = [];
          model.favorites.forEach((x){
              result.add(PopupMenuItem<String>(value: x, child: Row(
                  children: [
                      IconButton(icon: Icon(Icons.delete_outline), onPressed: (){
                          model.removeFavorite(x);
                          _favoritesKey.currentState?.setState((){});
                          setState(() {});
                      }),
                      Text(x)
                  ]
              )));
          });
          return result;
      },
      onSelected: (x){
          // Do something with the selected value
      },
  )

How can I make the popup menu rebuild itself while it is opened?

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • did u find a solution? i am at the same situation. I have a list in a PopupMenubtn and i want to delete an item in the popupmenu and it should update while its open. – data.firstName Oct 13 '22 at 11:55

0 Answers0