Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Null Check Operator used on null value – flutter

I am creating a drawer in flutter but whenever I click on the menu to open it, I get a Null check operator used on a null value error. What could be wrong?

        final scaffoldKey = GlobalKey<ScaffoldState>(); // the scaffoldKay variable


         AppBar(
          leading: IconButton(
            onPressed: () {
              scaffoldKey.currentState!.openDrawer(); // Error Here
            },
            icon: Icon(
              Icons.menu,
            ),
          ),
          title: Text(
            'Title Here',
            
            align: TextAlign.center,
            family: 'Poppins',
            style: FontStyle.normal,
            shadow: 0,
          ),
          

I have read others with a similar issues but it seems this is unique.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should assign scaffoldKey to the property key of the Scaffold widget. It’s going to be something like this:

return Scaffold(
  key: scaffoldKey,     // <- Here
  appBar: AppBar(
    leading: IconButton(
      onPressed: () {
        scaffoldKey.currentState!.openDrawer();
      },
      icon: Icon(
        Icons.menu,
      ),
    ),
    title: Text(
      'Title Here',
      align: TextAlign.center,
      family: 'Poppins',
      style: FontStyle.normal,
      shadow: 0,
    ),
  ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading