【发布时间】: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();
},
)
);
}
}
【问题讨论】: