I don't understand how we are supposed to assign a UniqueKey
to an object if the compiler doesn't let us.
This code fails if I use the second option:
body: const Center
(
child: Note(key: ValueKey('note'), initialValue: 'test'), // WORKS
child: Note(key: UniqueKey(), initialValue: 'test'), // ERROR
),
class Note extends StatefulWidget
{
final String initialValue;
const Note({required Key key, this.initialValue = ""}) : super(key: key);
gives this error:
Error: Cannot invoke a non-'const' constructor where a const expression is
expected.
Try using a constructor or factory that is 'const'.
How can I change this to allow UniqueKey to work please?
CodePudding user response:
You need to remove const
form your code as const requires a constant while while UniqueKey() changes itself.
body: Center
(
child: Note(key: ValueKey('note'), initialValue: 'test'), // WORKS
child: Note(key: UniqueKey(), initialValue: 'test'), // ERROR
),