flutterdart

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.


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.