【问题标题】:if (value.isEmpty) isEmpty is not working how i fix thisif (value.isEmpty) isEmpty 不起作用我如何解决这个问题
【发布时间】:2022-01-25 20:30:20
【问题描述】:

我尝试使用 ? 进行修复。和 !。和 !!他们都没有工作

并且错误消息是无法无条件访问属性“isEmpty”,因为接收者可以为“null”。 尝试使访问有条件(使用'?.')或向目标添加空检查('!')。

    return TextFormField(
      keyboardType: TextInputType.emailAddress,
      validator: (value) {
        if (value.**isEmpty**) {
          setState(() {
            errors.add("Please enter your email");
          });
        }
        return null;
      },
      decoration: const InputDecoration(
        labelText: "Email",
        hintText: "Enter your email",
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon: CustomSurffixIcon(
          svgIcon: "assets/icons/Mail.svg",
        ),
      ),
    );
  }
}

【问题讨论】:

标签: flutter dart


【解决方案1】:

如果你打电话

if(value.isEmpty)

且值为空,将检查什么条件? 试试

if(value != null && value.isEmpty)

或见:https://stackoverflow.com/a/52948927/5619109

if(value?.isEmpty ?? true)

【讨论】:

  • 所有这些都不起作用 :'(
【解决方案2】:

我推荐使用Flutter Form

像上面这样创建一个函数,自己自定义它。

  String? _validateInput(String? value) {
    if (value == null || value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  }

然后用这个函数来验证,比如

return TextFormField(
      keyboardType: TextInputType.emailAddress,
      validator: _validateInput,
      decoration: const InputDecoration(
        labelText: "Email",
        hintText: "Enter your email",
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon: CustomSurffixIcon(
          svgIcon: "assets/icons/Mail.svg",
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2019-11-07
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多