[dart] Invalid constant value.[dart] Arguments of a constant creation must be constant expressions.

我想使DropdownButton,但errorText只接受常量。

[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()
            )
        )
      )
    );

最佳答案

删除const之前的InputDecoration

decoration: InputDecoration(labelText: 'Gender', errorText: errorGender)

关于flutter - InputDecoration errorText必须为常数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52718061/

10-11 19:22