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

is there a 'invisible' constant like key-word in flutter?

i’m new and was lurking around with listview when i cameacross ‘OnTap’ function but when i implement i get the error "Invalid constant value" I have to everytime click on QuickFix any solution for this? i’ve attached the image and the code for the Widget

image of code snippet

class navDrawer extends StatelessWidget {
  const navDrawer({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Column(children: <Widget>[
        Expanded(
          child: ListView(padding: EdgeInsets.zero, children: <Widget>[
            const DrawerHeader(
              decoration: BoxDecoration(
                color: Colors.amber,
              ),
              child: const Text(
                'Labels',
                style: TextStyle(fontSize: 50, color: Colors.black),
              ),
            ),
            ListTile(
              selectedColor: Colors.deepOrange,
              selected: true,
              focusColor: Colors.deepOrange,
              leading: const Icon(Icons.label_important_outline),
              title: const Text(
                'Temporary',
                style: TextStyle(fontSize: 30, color: Colors.blueGrey),
              ),
              onTap: () {
                //made it work by quickfix
                print('Temporary');
              },
            ),
            const ListTile(
              selectedColor: Colors.lightBlue,
              leading: Icon(Icons.label_important),
              title: const Text(
                'Important',
                style: TextStyle(fontSize: 30, color: Colors.blueGrey),
              ),
              focusColor: Colors.cyan,
              onTap:(){
                //the one in the image
              },
            ),
            const ListTile(
              leading: Icon(Icons.label_important),
              title: const Text(
                'Health',
                style: TextStyle(fontSize: 30, color: Colors.blueGrey),
              ),
              focusColor: Colors.cyan,
            ),
            const ListTile(
              leading: Icon(Icons.label_important),
              title: const Text(
                'Personal',
                style: TextStyle(fontSize: 30, color: Colors.blueGrey),
              ),
              focusColor: Colors.cyan,
            )
          ]),
        ),
      ]),
    );
  }
}

i tried to understand the problem and hoping for a solution.

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 :

The const that is going to be deleted is the one before the ListTile. Basically, ListTile has a constant constructor because onTap is null by default (and null is a constant) however when you add a function in the onTap argument, it will no longer be constant (since functions are not constant). Now you ListTile is no longer constant hence you will have to remove the const declaration before ListTile.

Don’t worry, this will happen very often when working with Flutter.

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