简体   繁体   English

我想验证 flutter 中的用户输入

[英]I want to validate the user input in flutter

I'm trying to validate different user inputs, and i want to inform them which input is null i tried to show an alert dialog whenever the user hit the submit button while an input is null, but then i got an error and it didn't open the page saying i have to fill the information before i even open the page.我正在尝试验证不同的用户输入,并且我想告诉他们哪个输入是 null 我试图在用户点击提交按钮时显示一个警报对话框,而输入是 null,但后来我得到了一个错误,它没有t 打开页面说我必须在打开页面之前填写信息。 However i tried a something else but unfortunately it didn't work ether this is what i did.但是我尝试了其他方法,但不幸的是它不起作用,这就是我所做的。

 TextFormField(
                    key: _formKey,
                    validator: (value) {
                      if (value == null) {
                        return 'please write a review';
                      } else if (DATA == null) {
                        return 'please select a category';
                      } else if (image == null) {
                        return 'please add an image';
                      } else if (tag == null) {
                        return 'please select sub category';
                      } else if (_descriptionController.text == null) {
                        return 'please select a location';
                      } else
                        return null;
                    },
                    maxLines: 20,
                    maxLength: 200,
                    controller: _descriptionController2,
                    decoration: InputDecoration(
                      contentPadding:
                          EdgeInsets.only(left: 10.0, top: 16.0),
                      hintText: "What do you think about the place?",
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(16.0)),
                    ),
                  ),

That's what i added to the button when it's pressed这就是我按下按钮时添加的内容

onPressed: () {
          bool valid = validation();
          if (valid)
            addData(image!, DATA!, lat, lng, _descriptionController2.text,
                tag!, loggedInUser2!);
          Navigator.pop(context);
        },

lastly this is the validation method最后这是验证方法

bool validation() {
final form = _formKey.currentState;
if (form!.validate()) {
  return true;
}
return false;

} }

can any one help?有人可以帮忙吗?

i think is unnecessary for doing validation of DATA,image, etc for description field.我认为对描述字段的数据、图像等进行验证是不必要的。 TextFormField will return String, it will always false when you do validation for DATA, image, etc. TextFormField将返回 String,当您对 DATA、图像等进行验证时,它将始终为 false。

just do like this:这样做:

Form(
  key:_formKey,
  child: Column(
    children:[
       TextFormField(
          validator: (String? value) {
            //  runType value is String? 
            //  which is it always false when you compare to image, or Data.
            if (value == null) {
              return 'please write a description';
            else
              return null;
            },
          controller: _descriptionController2,
        ),

then if you want to do validation for other data, i think you can make it manually.那么如果你想对其他数据进行验证,我认为你可以手动进行。

if (DATA== null) {
  //do something 
}
...etc

The validator(String? value) in widget TextFormField should validate ONLY for its text value.小部件TextFormField中的validator(String? value)验证其文本值。 Other fields like Data , image ... should be handled in another place.其他字段,如Dataimage ... 应该在另一个地方处理。

I'm also working on validator for TextFormField and I realize that we shouldn't check null.我也在研究TextFormFieldvalidator ,我意识到我们不应该检查 null。 Because when you first time navigate to this screen, the TextFormField will immediately show red error message below and it could make user uncomfortable.因为当您第一次导航到此屏幕时, TextFormField会立即在下方显示红色错误消息,这可能会让用户感到不舒服。 You should check null only when user press "Save/Comment" button and show popup/snack bar if the text is null/empty.只有当用户按下“保存/评论”按钮并在文本为空/空时显示弹出/小吃栏时,您才应该检查 null。

Also I got some tricks that you may interest:我也得到了一些你可能感兴趣的技巧:

  • Use extension (extension for String? ) if you has some special cases can be used in future, like: valid email, valid password (8 character, special symbols)...使用扩展名( String? )如果您将来有一些特殊情况可以使用,例如:有效的 email,有效的密码(8 个字符,特殊符号)...

  • Don't forget to use function .trim() to remove white space on textEditingController.不要忘记使用 function .trim()删除 textEditingController 上的空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想验证 flutter 中的“文本”不是(TextFormField 或 Textfield) - I want to validate "Text" in flutter not (TextFormField or Textfield) 在 Firebase Authenticator Flutter 之前验证用户输入 - Validate user input before Firebase Authenticator Flutter 我想在 Flutter 中验证我的 textFormField 而不改变装饰 - I want to validate my textFormField in Flutter without changing the decoration 我想在 flutter 中创建用户配置文件列表? - i want to create a list of user profile in flutter? 验证超出输入重试 Flutter - Validate Exceed Input Retry Flutter 我想用iconButton验证我的颤动表单并转到主页。 但我无法将按钮链接到我想要显示的页面 - I want to validate my flutter form with iconButton and go to the homepage. But i couldn't link the button with the page i want to display 在Flutter,我想在每天的某个时间给用户发送一个通知 - In Flutter, I want to send a notification to the user every day at a certain time 我想知道用户是否已经在 flutter 中对我的应用进行了评分? - i want to know Whether user rated my app already or not in flutter? Flutter - 我想把登录抽屉的用户的名字和 email - Flutter - I want to put the name and email of the user logged in the drawer 我想在 flutter 中使用 11 位正则表达式验证电话号码,并且应始终以 09 开头 - I want to validate a phone number using regexp in flutter with 11 digit and should always start with 09
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM