错误“无法无条件访问属性'isEmpty',因为接收者可以为'null'”有解决办法吗?

时间:2021-06-03 05:04:38

标签: flutter dart

“The property 'isEmpty' can't be unconditionally access because thereceiver can be 'null'.” 错误有没有解决办法。

我的代码

new TextFormField(
                            decoration: new InputDecoration(
                              icon: Icon(
                                Icons.email_outlined,
                                color: Color(0xff979694),
                              ),
                              labelText: 'Email',
                              fillColor: Color(0xff979694),
                              border: new OutlineInputBorder(
                                borderRadius: new BorderRadius.circular(25.0),
                                borderSide: new BorderSide(),
                              ),
                            ),
                            validator: (val) => val.isEmpty ? 'Enter an email' : null,
                            keyboardType: TextInputType.emailAddress,
                            style: new TextStyle(
                              fontFamily: "GilroyLight",
                              fontWeight: FontWeight.bold,
                              color: Color(0xff979694),
                            ),

                            onChanged: (val){
                              setState(() => email = val);
                            },
                          ),

我在行中遇到错误

validator: (val) => val.isEmpty ? 'Enter an email' : null,

3 个答案:

答案 0 :(得分:1)

试试这个,如果 val 为空,它会返回“输入电子邮件”

() => val?.isEmpty?? true ? 'Enter an email' : null

答案 1 :(得分:0)

你可以检查它是否为空

 validator: (val) {  
    if(val != null){
        val.isEmpty ? 'Enter an email' : null,
     }
   }

答案 2 :(得分:0)

添加为空时的条件

validator: (val) => (val==null || val.isEmpty) ? 'Enter an email' : null,