Flutter_form_builder错误

ilmyapht  于 2023-05-23  发布在  Flutter
关注(0)|答案(2)|浏览(146)

我尝试使用flutter_form_builder: ^7.1.0form_builder_validators来创建一个表单,但是在
onChanged: _onChanged,validator: FormBuilderValidators.compose
我试着跟随flutter_form_builder的例子,但是这个例子似乎不完整或者不是最新的。当我尝试添加autovalidate: true,时,我得到另一个未定义的错误。我该怎么解决这个问题?

class _CreateCompanyViewState extends State<CreateCompanyView> {

  @override
  void initState() {
    super.initState();
  }

  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
FormBuilder(
                 
                key: _formKey,

                child: Column(
          
                  children: <Widget>[

                    FormBuilderTextField(

                      name: 'age',          

                      decoration: InputDecoration(
                     
                      labelText: 'This value is passed along to the [Text.maxLines] attribute of the [Text] widget used to display the hint text.',
                      
                      ),
              
                      onChanged: _onChanged,
            
                        validator: FormBuilderValidators.compose([
               
                          FormBuilderValidators.required(context),

                          FormBuilderValidators.numeric(context),
                        
                          FormBuilderValidators.max(context, 70),
               
                        ]),
              
                          keyboardType: TextInputType.number,
            
                        ),

                  Row(

                    children: <Widget>[
        
                      Expanded(
            
                        child: MaterialButton(
              
                          color: Theme.of(context).colorScheme.secondary,
              
                        child: Text("Submit", style: TextStyle(color: Colors.white),),

                          onPressed: () {
                
                            _formKey.currentState?.save();
                
                            if (_formKey.currentState!.validate()) {
                  
                              print(_formKey.currentState!.value);
                
                            } else {
                  
                              print("validation failed");
                            }
                          },
                        ),
                      ),
          
                      SizedBox(width: 20),
          
                      Expanded(
            
                        child: MaterialButton(

                          color: Theme.of(context).colorScheme.secondary,
              
                          child: Text("Reset", style: TextStyle(color: Colors.white),),
              
                          onPressed: () {

                            _formKey.currentState!.reset();
                          },
                        ),
                      ),
                    ],
                  )
                ],
              )
            ),
            ],
          ),
        ),
      ),
    )));
  }
x3naxklr

x3naxklr1#

FormBuilderValidators不处理空值。您需要检查空值,然后执行其他验证器。

validator: FormBuilderValidators.compose([
  (val) {
    return val == null ? "Field is empty" : null;
  },
  FormBuilderValidators.required(context), 
/// others validator
]),
8yparm6h

8yparm6h2#

如果你最近的form_builder_validators出现问题,这意味着你正在使用一个仍然有bug的新版本form_builder_validators。
怎么办,你必须revers你的Flutter版本和下载Fluttersdk 3.7.12下面的链接3.7.12 DOWNLOAD Flutter SDK - V 3.7.12

然后也降级您的软件包以

flutter_form_builder: ^7.7.0
  form_builder_validators: ^8.3.0

至少我相信这两个工作正常。

相关问题