[dart] Invalid constant value. [dart] Arguments of a constant creation must be constant expressions.
I want to make DropdownButton , but errorText only accept constant variable.
[dart] Invalid constant value. [dart] Arguments of a constant creation must be constant expressions.
Constant variable means I cannot replace with other text.
Maybe any other way to make DropdownButton validation?
String errorGender = null;
var _inputGender = InputDecorator(
decoration: const InputDecoration(labelText: 'Gender', errorText: errorGender),
isEmpty: data['gender'] == null,
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isDense: true,
value: data['gender'],
onChanged: (value) => setState(() => data['gender'] = value),
items: _gender.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value[0].toUpperCase() + value.substring(1)),
);
}).toList()
)
)
)
);
In dart when you pass something as a parameter in a const constructor, the compiler makes sure that the value set as default is not changed during the execution of the code. Hence, the Invalid constant value warning. To resolve this issue you should remove the const keyword from the in front of the Text.
InputDecoration class Null safety The border, labels, icons, and styles used to decorate a Material Design text field. The TextField and InputDecorator classes use InputDecoration objects to describe their decoration.
By default, a TextField is decorated with an underline. You can add a label, icon, inline hint text, and error text by supplying an InputDecoration as the decoration property of the TextField . To remove the decoration entirely (including the underline and the space reserved for the label), set the decoration to null.
Remove const
before InputDecoration
decoration: InputDecoration(labelText: 'Gender', errorText: errorGender)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With