Search code examples
flutter

onPressed "invalid constant value" error in Flutter


There appears to be nothing wrong with the code I have for my ElevatedButton, but It's giving me this error:

        TextButton(
          onPressed: () { },
          child: Text('TextButton'),  
        )

enter image description here

This is even posted from the official Flutter website.

Any idea why it's doing this?


Solution

  • It's suspected that your button is a child of a widget that's marked as constant widget, if so, just remove const keyword from parents of that button.

    That's because onPressed() can't be declared on a constant scope.