来自状态小部件的父级回调函数在null上调用了“呼叫”

时间:2019-08-30 12:50:04

标签: flutter widget

我想要一个包含多个子表单的多部分表单,当完成并通过验证后,这些子表单将回调到父窗口小部件,以转到下一个子表单。

我的问题是我不断收到错误消息,说该函数为null,我不确定为什么。

iv尝试在孩子的StatefulWidget中包含一个函数,但是当调用通过“ complete”函数传递的父母时,我总是收到一条错误消息,说该函数为null。

I/flutter (23821): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (23821): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (23821): The method 'call' was called on null.
I/flutter (23821): Receiver: null
I/flutter (23821): Tried calling: call()

我以为我做得正确,但是接下来要尝试的内容有点迷茫,这是我的代码:

父窗口小部件


class CreateForm extends StatefulWidget {

  CreateForm({
    Key key
  }): super(key: key);

  CreateFormState createState() => CreateFormState();
}

class CreateFormState extends State < CreateForm > {

  bool userDetailsCompleted = false;

  final formKey = GlobalKey < FormState > ();

  @override
  Widget build(BuildContext context) {
    return !userDetailsCompleted ? UserDetailsForm(
      completed: () {
        print("testing");
        // setState(() {
        //   userDetailsCompleted = true;
        // });
      },

    ) : nextSection(

    );
  }
}

子小部件


class UserDetailsForm extends StatefulWidget {

  final Function completed;

  UserDetailsForm({
    @required this.completed,
    Key key
  }): super(key: key);

  UserDetailsFormState createState() => UserDetailsFormState();

}

class UserDetailsFormState extends State < UserDetailsForm > {

  @override
  Widget build(BuildContext context) {
    return Form(
        child: CustomButton(
              size: 15,
              text: 'Submit',
              onPressed: () {
                print('UserDetails account form submit');
                widget.completed();
              },
            )
          );
        }
    }


1 个答案:

答案 0 :(得分:0)

我在本地尝试了您的代码,并且可以正常工作。我认为您只需要重新启动Flutter,因为也许刚才您将Stateles替换为Stateful小部件

全面工作的仓库

您也可以在此Github repo

中本地构建它

演示

Code