Home > Enterprise >  Why isn't popup menu button working in flutter?
Why isn't popup menu button working in flutter?

Time:03-08

I am trying to use PopupMenuButton but it is throwing the error message:

"A value of type 'Null' can't be assigned to a parameter of type 'List<PopupMenuEntry<Text>> Function(BuildContext)' in a const constructor."

How can I fix this issue?

This is my code:

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const IconButton(
          icon: Icon(Icons.menu),
          onPressed: null,
          tooltip: 'Navigation menu',
        ),
        title: const Text('FlashyCardy'),
        actions: const [
          IconButton(
            icon: Icon(Icons.search),
            tooltip: 'Search',
            onPressed: null,
          ),
          IconButton(
            onPressed: null,
            tooltip: 'Add task',
            icon: Icon(Icons.add_task),
          ),
          PopupMenuButton<Text>(
            itemBuilder: (context) => <PopupMenuEntry<Text>>[
              const PopupMenuItem<Text>(
                child: Text('Working a lot harder'),
              ),
            ];
          ),
        ],
      ),
      body: null,
    );
  }
}

CodePudding user response:

You're using const incorrectly, use const after creating the list before creating the widgets. There are other problems with the way have used const, I have modified your code to create a complete working example:

class MyHomePage extends StatelessWidget {    
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const IconButton(
          icon: Icon(Icons.menu),
          onPressed: null,
          tooltip: 'Navigation menu',
        ),
        title: const Text('FlashyCardy'),
        actions:  [
          const IconButton(
            icon: Icon(Icons.search),
            tooltip: 'Search',
            onPressed: null,
          ),
          const IconButton(
            onPressed: null,
            tooltip: 'Add task',
            icon: Icon(Icons.add_task),
          ),
          PopupMenuButton<Text>(
            itemBuilder: (context) => <PopupMenuEntry<Text>>[
               const PopupMenuItem<Text>(
                child: Text('Working a lot harder'),
              ),
            ]),
        ],
      ),
      body: null,
    );
  }
}
  • Related