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
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.
2
Answers
The
const
that is going to be deleted is the one before theListTile
. Basically,ListTile
has a constant constructor becauseonTap
is null by default (and null is a constant) however when you add a function in theonTap
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 theconst
declaration beforeListTile
.Don’t worry, this will happen very often when working with Flutter.
That hint refers to the
const ListTile(...
in the code you shared. From the looks of it, once you add anonTap
to the list tile widget it can no longer beconst
.