颤振未将返回块传递给表单+按钮中的onPressed字段,此代码有什么问题?

时间:2019-07-09 23:58:30

标签: flutter dart

我正在编写一个移动应用程序。作为其中的一部分,我有一个需要验证其字段的表单。我为此使用了一个Form()。当我使用应用程序的调试版本时,一切工作正常。表单正在更新,验证和保存。但是,当我构建该应用程序的发行版时,该表单停止正常工作。下面,我提供了一些错误的代码(该代码之前运行良好,并且在构建发行版应用程序后立即停止运行,而没有明显原因)。

当我点击表单上的一个按钮时,_validateAndSubmitComment函数被调用(下面提供)。在代码中,您会注意到我也添加了一些打印语句,以了解我要去的地方。

_validateAndSubmitComment() {
      print("Trying to validate things here!");
      if(widget.isSolvedClient)
        return null;
      else {
        print("Made it till here...");
        return () {
          print("Something happened alright...");
          final form = _formKeyComments.currentState;
          if(form.validate()) {
            print("Trying to make this work!");
            form.save();
            form.reset();
            }
        };
      }
    }

当我按下按钮时,“尝试在此处验证内容!”被打印出来,“ Made it to here ...”也是如此(因为目前widget.isSolvedClient设置为true),但这是使事情变得有趣的地方。返回代码段中的下一条打印消息应该并且早些时候已执行,但现在根本没有。字符串“发生了什么事...”从未输出到控制台。由于我的form.save()函数调用在其中,因此这使按钮无用。

编辑:如果widget.isSolvedClient为true,那么我将返回null,以便禁用该按钮。如果不是真的,我希望return块得到执行,但是现在不再发生了。另外,我在工作时更新频繁,我不知道这是否与此有关。请帮忙。

如果有帮助,这是表单本身的代码:

Form commentsForm = new Form(
      key: _formKeyComments,
      child: Column(
        children: <Widget>[
          TextFormField(
            decoration: new InputDecoration(labelText: "Comments"),
            onSaved: (String value) { print("Trying to save here!"); addComments(widget.docID, value); },
            validator: _validateCommentsField
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center, 
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(10.0),
                  child: FlatButton(
                    color: Colors.lightBlue,
                    textColor: Colors.white,
                    child: Text("Add comment"),
                    onPressed: _validateAndSubmitComment,
                    ),
                  ),
                Padding(
                  padding: EdgeInsets.all(10.0),
                  child: FlatButton(
                    color: Colors.lightBlue,
                    textColor: Colors.white,
                    child: Text("Cancel"),
                    onPressed: () => Navigator.of(context).pop(),
                    ),
                  ),
              ],
            ),
          ],
        ),
      );

_formKeyComments在状态类本身内部初始化,所有其他代码也都驻留在该状态类中(上面的代码在Widget build(...)函数中),如下所示:

class CommentsPageState extends State<CommentsPage> {
  final _formKeyComments = new GlobalKey<FormState>();

0 个答案:

没有答案