【问题标题】:'call' was called on null for parent callback function from stateful widget有状态小部件的父回调函数在 null 上调用了“调用”
【发布时间】:2020-01-03 17:29:27
【问题描述】:

我想要一个包含多个子表单的多节表单,这些子表单在完成和验证后将回调到父窗口小部件以便转到下一个子表单。

我的问题是我不断收到错误消息,说函数为空,我不知道为什么。

iv 尝试在子的 StatefulWidget 中使用一个函数,但在调用通过“完整”函数传递的父级时,我总是收到一个错误,指出该函数为空。

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();
              },
            )
          );
        }
    }


【问题讨论】:

    标签: flutter widget


    【解决方案1】:

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

    完全正常的存储库

    你也可以在这个Github repo本地构建它

    演示

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多