【问题标题】:Pass a Var to Constant Parameter in Flutter将 Var 传递给 Flutter 中的常量参数
【发布时间】:2021-11-08 23:11:44
【问题描述】:

当我编写此代码时,

        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),
              ),
            )

而我的 Texts 类是这样的,

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

我收到一个错误,

Invalid constant value.dart(invalid_constant)

这些 helperTexthintTexthelperText 需要常量字符串,但我想使用 List 将它们传递给这样的类,不要直接指定值。

那么,我该怎么办?

请帮帮我!我是 Flutter 和 Dart 的新手!!!

【问题讨论】:

    标签: flutter dart type-conversion constants var


    【解决方案1】:

    从父级移除const,即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),
              ),
            )
    

    【讨论】:

    • 成功了!,哦,我真是个傻瓜XD,非常感谢你的帮助!!,我真的很感激......
    • 谢谢兄弟 :-)
    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 2011-03-30
    • 2017-07-12
    • 2018-08-20
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多