flutter - value.isEmpty 在我尝试编译代码时抛出错误
问题描述
TextFormField(
obscureText: true,
decoration: InputDecoration(
hintText: "Enter password",
labelText: "Password",
),
validator: (value) {
if (value.isEmpty) {
return "Password can't be empty";
}
null;
},
),
我正在看一个关于颤振的教程,我知道它太基础了,但似乎无法修复这个错误但不适合我,我还不明白它显示The property 'isEmpty' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').
这个错误
解决方案
这是因为value
is 类型String?
并且可以为 null。在你的情况下,你必须检查 if value
is null
。
TextFormField(
obscureText: true,
decoration: InputDecoration(
hintText: "Enter password",
labelText: "Password",
),
validator: (String? value) {
if (value!.isEmpty) {
return "Password can't be empty";
}
null;
},
),
推荐阅读
- vba - 使用 vlookup 结果更新列数据的宏
- java - 身份验证处理程序中的 Spring 自动装配
- openstack - 重启nova计算服务的影响
- pandas - pandas:to_csv 附加模式,保留列顺序
- javascript - Javascript翻转时钟倒计时生成不正确的日期格式
- java - json 解析错误“NullPointerException”
- forms -
- python - 无法通过 Jenkins 声明式管道在 Docker 映像中作为代理进行 pip 安装
- php - 在 Woocommerce 结帐页面中使用 AJAX 和费用 API 的动态折扣
- xcode - Xcode Source Editor 选项未显示在 Extensions 中