You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何TextFormField设置多行时出现‘加密字段不可多行’报错?

Flutter TextFormField 多输入行报错问题处理

问题代码

TextFormField(
  keyboardType: TextInputType.multiline,
  maxLines: 4,
  decoration: InputDecoration(
    filled: true,
    fillColor: AppColors.ARGB.withOpacity(0.3),
    isDense: true,
    focusedBorder: const OutlineInputBorder(
        borderSide: BorderSide(
      width: 1,
    )),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(
          DimensionsResource.RADIUS_EXTRA_SMALL),
      borderSide: BorderSide.none,
    ),
    contentPadding: const EdgeInsets.only(
        top: 16, bottom: 16, left: 10),
    hintText: 'Enter Detected Box IDs',
    hintStyle: const TextStyle(
      color: AppColors.WHITE,
      fontSize: DimensionsResource.FONT_SIZE_SMALL_2X,
      fontWeight: FontWeight.w200,
    ),
  ),
),

报错信息

Obscured fields cannot be multiline. Failed assertion: line 171 pos 15: '!obscureText || maxLines == 1'

错误原因

Flutter的TextFormField内置断言校验:若开启obscureText(内容隐藏,如密码输入),则必须设置maxLines: 1。你的代码未显式设置obscureText,但它可能被默认设为true(比如继承父组件配置、框架默认值被意外修改),与maxLines:4的设置冲突,触发断言报错。

解决方法

根据需求选择对应方案:

  1. 不需要隐藏输入内容(当前场景输入Box IDs)
    显式添加obscureText: false覆盖默认值:
TextFormField(
  keyboardType: TextInputType.multiline,
  maxLines: 4,
  obscureText: false, // 新增该行
  decoration: InputDecoration(
    filled: true,
    fillColor: AppColors.ARGB.withOpacity(0.3),
    isDense: true,
    focusedBorder: const OutlineInputBorder(
        borderSide: BorderSide(
      width: 1,
    )),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(
          DimensionsResource.RADIUS_EXTRA_SMALL),
      borderSide: BorderSide.none,
    ),
    contentPadding: const EdgeInsets.only(
        top: 16, bottom: 16, left: 10),
    hintText: 'Enter Detected Box IDs',
    hintStyle: const TextStyle(
      color: AppColors.WHITE,
      fontSize: DimensionsResource.FONT_SIZE_SMALL_2X,
      fontWeight: FontWeight.w200,
    ),
  ),
),
  1. 确实需要隐藏输入内容
    将maxLines改为1,Flutter不支持多行隐藏输入场景:
TextFormField(
  keyboardType: TextInputType.text,
  maxLines: 1, // 修改为1
  obscureText: true, // 确保开启隐藏
  // 其余配置保持不变
),

内容的提问来源于stack exchange,提问作者faizan Muzamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:24:55