【问题标题】:Is there a solution to the error "The property 'isEmpty' can't be unconditionally accessed because the receiver can be 'null'."是否有解决错误“无法无条件访问属性'isEmpty',因为接收者可以是'null'。”
【发布时间】:2021-06-03 05:04:38
【问题描述】:

报错“属性‘isEmpty’不能无条件访问,因为receiver可以是‘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,

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以检查它是否为空

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

      【讨论】:

        【解决方案3】:

        添加为空的条件

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

        【讨论】:

          猜你喜欢
          • 2022-08-15
          • 2021-08-05
          • 2021-09-21
          • 2021-07-13
          • 2022-08-15
          • 2022-01-11
          • 1970-01-01
          • 2022-08-15
          • 2021-08-12
          相关资源
          最近更新 更多