Search code examples
flutterlistdartflutter-list-tile

Invalid constant value for a list item value that I am trying to determine its Boolean value to decide Font weight of list tile


Invalid constant error message for item.header.

It is a boolean value representing if the list tile is going to have a bold or normal font-weight.

 child: ListTile(
        key: ValueKey(item.stuffId),
        title: Text(
          item.name,
          style: const TextStyle(
            color: Colors.blue,           
            fontWeight: item.header == true ? FontWeight.bold : FontWeight.normal,                   
          ),
        ),

Here is the list that I used:

  final List<Stuff> _items = [
    Stuff(stuffId: "1", name: "Toiletries", header: true),
    Stuff(stuffId: "2", name: "Toothpaste", header: false),
    Stuff(stuffId: "3", name: "Hair brush", header: false),
    Stuff(stuffId: "4", name: "Nail clippers", header: false),
  ];

enter image description here

'Toiletries' is the header and therefore supposed to be bold.


Solution

  • ListTile(
            key: ValueKey(item.stuffId),
            title: Text(
              item.name,
              style: TextStyle( // 👈 remove const here 
                color: Colors.blue,           
                fontWeight: item.header == true ? FontWeight.bold : FontWeight.normal,                   
              ),
            ),