Search code examples
flutterdarttype-conversionconstantsvar

Pass a Var to Constant Parameter in Flutter


When I write this Code,

        TextFormField(
              cursorRadius: Radius.circular(26),
              cursorColor: Colors.green,
              onChanged: onTextInChange,
              textInputAction: TextInputAction.search,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: texts.toForm[0],
                hintText: texts.toForm[1],
                helperText: texts.toForm[2],
                labelStyle: TextStyle(fontSize: 18),
                hintStyle: TextStyle(fontSize: 18),
              ),
            )

And my Texts Class is look like this,

class Texts{
   let toForm = ['Enter Your Name', 'John Fernando', 'This is a Helper Text'];
}

I got an error called,

Invalid constant value.dart(invalid_constant)

These helperText, hintText, helperText are expecting constant Strings, but I want to pass them through a class like this using a List, not to Directly Specify the Value.

So, what should I do?

Please help me! I'm new to Flutter and Dart!!!


Solution

  • Remove the const from parent i.e InputDecoration

            TextFormField(
              cursorRadius: Radius.circular(26),
              cursorColor: Colors.green,
              onChanged: onTextInChange,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                border: UnderlineInputBorder(),
                labelText: texts.toForm[0],
                hintText: texts.toForm[1],
                helperText: texts.toForm[2],
                labelStyle: TextStyle(fontSize: 18),
                hintStyle: TextStyle(fontSize: 18),
              ),
            )