如果 body:Form 没有在颤振应用程序中使用,我的表单数据没有被提交或保存

Posted

技术标签:

【中文标题】如果 body:Form 没有在颤振应用程序中使用,我的表单数据没有被提交或保存【英文标题】:My Form data is not getting submitted or saved if body:Form is not used in flutter appplication 【发布时间】:2021-04-02 17:20:20 【问题描述】:

如果 body:Form 未在 Flutter 应用程序中使用,我的表单数据不会被提交或保存。

如果使用 body:Form 而不是 SingleChildScrollView 我的整个表单页面或包含 12 到 13 个文本项的小部件不可见,包括“上传”或“提交”按钮。

这是我的代码

  body: Form(
      key: formKey,
        child: Center(
        child: Column(
    children: <Widget>[
    SizedBox(height: 20,), //just for spacing
    Column(
    mainAxisAlignment: MainAxisAlignment.start,

    children: <Widget>[
    Padding(padding: EdgeInsets.only(top: 15)),
              Container(
                child: imageFile==null?
                FlatButton(
                    onPressed: ()
                      _showDialog();
                    ,
                    child: Icon(Icons.add_a_photo,size: 80,color: Color(0xffff2fc3),)
                ):
                Image.file(imageFile,width: 200,height: 100,),
              ),

              SizedBox(height: 10,),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                    child: Theme(
                      data: ThemeData(
                        hintColor: Colors.blue,
                      ),
                      child: TextFormField(
                        validator: (value)
                          if(value.isEmpty)
                            return "Please write  a";
                          else 
                            a = value;
                          
                        ,

                        keyboardType: TextInputType.name,
                        autofocus: false,
                        decoration: InputDecoration(
                          labelText: 'A',
                        
                        ),
                        maxLength: 15,


                      ),
                    ),
                  ),
                  ),


                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                        validator: (value)
                          if(value.isEmpty)
                            return "Please write the b";
                          else 
                            b= value;
                          
                        ,

                        keyboardType: TextInputType.name,
                        autofocus: false,
                        decoration: InputDecoration(
                          labelText: 'B',
                        
                        ),
                        maxLength: 15,


                        ),

                      ),
                  ),
                  ),
],
      ),




      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                      child: TextFormField(
                        validator: (value)
                          if(value.isEmpty)
                            return "Please write the C";
                          else 
                           c = value;
                          
                        ,

                        keyboardType: TextInputType.name,
                        autofocus: false,
                        decoration: InputDecoration(
                          labelText: 'C',
                     
                        ),
                        maxLength: 15,


                      ),

                      ),
                    ),
                  ),



                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write the D";
                            else 
                              d = value;
                            
                          ,

                          keyboardType: TextInputType.name,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: 'D',
                           
                          ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),
],
      ),


      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write the E";
                            else 
                              e = value;
                            
                          ,

                          keyboardType: TextInputType.name,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: 'E ',
                           
                          ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),

                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write the F";
                            else 
                              f = value;
                            
                          ,

                          keyboardType: TextInputType.number,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: 'F',
                      
                          ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),

        ],
      ),


      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write the G";
                            else 
                             g = value;
                            
                          ,

                          keyboardType: TextInputType.name,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: ' G ',
                           
                          ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),

                      Expanded(
                        flex: 1,
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Theme(
                            data: ThemeData(
                              hintColor: Colors.blue,
                            ),

                            child: TextFormField(
                              validator: (value)
                                if(value.isEmpty)
                                  return "Please write H";
                                else 
                                  h = value;
                                
                              ,

                              keyboardType: TextInputType.name,
                              autofocus: false,
                              decoration: InputDecoration(
                                labelText: ' H  ',
                           
                              ),
                              maxLength: 15,


                            ),

                          ),
                        ),
                      ),
        ],
      ),




      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
                      Expanded(
                        flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write the I";
                            else 
                              i = value;
                            
                          ,

                          keyboardType: TextInputType.number,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: 'I',
                      
                        ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),





                  Expanded(
                    flex: 1,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Theme(
                        data: ThemeData(
                          hintColor: Colors.blue,
                        ),

                        child: TextFormField(
                          validator: (value)
                            if(value.isEmpty)
                              return "Please write J";
                            else 
                              j = value;
                            
                          ,

                          keyboardType: TextInputType.name,
                          autofocus: false,
                          decoration: InputDecoration(
                            labelText: ' J ',
                         
                          ),
                          maxLength: 15,


                        ),

                      ),
                    ),
                  ),
        ],
      ),

    
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
              Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Theme(
                    data: ThemeData(
                      hintColor: Colors.blue,
                    ),

                    child: TextFormField(
                      validator: (value)
                        if(value.isEmpty)
                          return "Please write the K";
                        else 
                          k = value;
                        
                      ,

                      keyboardType: TextInputType.name,
                      autofocus: false,
                      decoration: InputDecoration(
                        labelText: ' K ',
                   
                      ),
                      maxLength: 15,


                    ),

                  ),
                ),
              ),


                  SizedBox(width: 5,),



        ],
      ),






              RaisedButton(
                onPressed: ()

                  if(imageFile == null)
                    Fluttertoast.showToast(
                        msg: "Please select an image",
                        gravity: ToastGravity.CENTER,
                        toastLength: Toast.LENGTH_LONG,
                        timeInSecForiosWeb: 2
                    );
                  else 
                    upload();
                  

                ,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                color: Color(0xffff2fc3),
                child: Text("Upload",style: TextStyle(fontSize: 18,color: Colors.blue),),
              )


    ],//Widget
    ),//Column

    ],Widget

        ),//column

    ),//Center

    ),//Form
   
   
    );Scaffold
  

如果我使用了 body:SingleChildScrollView 我的表单小部件正在滚动,并且上传或提交按钮可见,但无法将表单数据保存或存储到 firebase 数据库中。

如果 body:Form ( ) 仅包含 5 个或更少的项目,则一切正常,因为我看到“上传”或“提交”按钮并能够提交表单数据。

请解决我的问题。

错误:当使用滚动视图并单击提交或上传按钮时

E/flutter (29120):[错误:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:NoSuchMethodError:方法“验证”在 null 上调用。 E/颤振(29120):接收器:空 E/flutter (29120): 尝试调用: validate() E/flutter (29120): #0 _UploadDataState.upload (package:scarves_store/UploadData.dart:587) E/颤振(29120):#1 _UploadDataState.build。 (包:scarves_store/UploadData.dart:519) E/flutter (29120): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992) E/颤振(29120):#3 _InkResponseState.build。 (包:flutter/src/material/ink_well.dart:1098) E/flutter (29120): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184) E/flutter (29120): #5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524) E/flutter (29120): #6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:301) E/flutter (29120): #7 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256) E/flutter (29120): #8 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158) E/flutter (29120): #9 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224) E/flutter (29120): #10 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200) E/flutter (29120): #11 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158) E/flutter (29120): #12 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104) E/flutter (29120): #13 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88) E/flutter (29120): #14 _rootRunUnary (dart:async/zone.dart:1206) E/flutter (29120): #15 _CustomZone.runUnary (dart:async/zone.dart:1100) E/flutter (29120): #16 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005) E/flutter (29120): #17 _invoke1 (dart:ui/hooks.dart:267) E/flutter (29120): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:176) E/颤动​​(29120):

【问题讨论】:

请添加upload();方法 Future upload() async if(formKey.currentState.validate()) StorageReference reference = FirebaseStorage.instance.ref().child("images") .child(new DateTime .now().millisecondsSinceEpoch.toString() + "." imageFile.path); StorageUploadTask uploadTask = reference.putFile(imageFile); var imageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();字符串 url = imageUrl.toString(); DatabaseReference databaseReference = FirebaseDatabase.instance .reference().child("Data"); String uploadId = databaseReference .push() .key; HashMap map = new HashMap();地图 ["cattegorrytype"] = cattegorrytype;地图 ["a"] = a;地图 ["b"] = b;地图[“c”] = c;地图[“d”] = d;地图[“e”] = e;地图[[“f”] = f;地图 ["g"] = g;地图 ["h"] =h;地图 ["j"] = j;地图 ["imgUrl"] = url,databaseReference.child(uploadId).set(map); 以上是我的upload();方法 【参考方案1】:

请试试这个:

body: SingleChildScrollView(
  child: Center(
   child: Form(
    key: formKey,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        SizedBox(height: 20,), //just for spacing
        .....// OTHER WIDGETS
      ],
     ),
    ),
  ),
),

【讨论】:

Ok Mohammed 我会在尝试您的代码后更新您,谢谢。 谢谢你 Mohammed ,你已经完全解决了我的问题。再次感谢。和我在一起请同时关注我在堆栈上的其他查询。如果我想就我的 Flutter 查询与您沟通,我应该如何联系您? 你可以回复我的stack账号 好的,请在这里回答***.com/questions/65435218/… Mohammad_Asef 请尝试在这里回答我的问题***.com/questions/65395772/…

以上是关于如果 body:Form 没有在颤振应用程序中使用,我的表单数据没有被提交或保存的主要内容,如果未能解决你的问题,请参考以下文章

如何在整个颤振项目中使用 json 对象之一?

使用颤振音频查询后没有在我的应用程序中获取歌曲文件

需要帮助在颤振代码中复制设计

如何在颤振中创建工具栏搜索视图

有没有办法使用 mediapipe 在颤振中实现手势检测?

有没有办法在颤振应用程序中使用firebase查找具有相同孩子名字的孩子的总数?