以下最低限度可重现的虚拟代码会引发此错误:
════════ Exception caught by widgets library ═══════════════════════════════════
Multiple widgets used the same GlobalKey.
════════════════════════════════════════════════════════════════════════════════
Restarted application in 987ms.
I/flutter (10106): Key: [LabeledGlobalKey<FormBuilderState>#070c0 GlobalFormKey #SignIn ]
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.
The key [LabeledGlobalKey<FormBuilderState>#070c0 GlobalFormKey #SignIn ] was used by multiple widgets. The parents of those widgets were:
- FormBuilderWrapper-[LabeledGlobalKey<FormBuilderState>#070c0 GlobalFormKey #SignIn ]
- _BodyBuilder
A GlobalKey can only be specified on one widget at a time in the widget tree.
虚拟代码:
class SignInScreen extends StatelessWidget {
final GlobalKey<FormBuilderState> key =
GlobalKey<FormBuilderState>(debugLabel: 'GlobalFormKey #SignIn ');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Dummy")),
body: FormBuilderWrapper(
key: key,
childrenInColumn: [
FormBuilderEmail(),
FormBuilderPassword(identifierForField: "password")
],
),
);
}
}
class FormBuilderWrapper extends StatelessWidget {
final List<Widget> childrenInColumn;
final Key key;
const FormBuilderWrapper({
@required this.key,
@required this.childrenInColumn,
});
@override
Widget build(BuildContext context) {
print("Key: $key");
return FormBuilder(
key: key,
child: Column(
children: this.childrenInColumn,
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class FormBuilderEmail extends StatelessWidget {
const FormBuilderEmail({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return FormBuilderTextField(
name: "email",
);
}
}
class FormBuilderPassword extends StatelessWidget {
final String hintText;
final String identifierForField;
const FormBuilderPassword({
@required this.identifierForField,
this.hintText = "Password",
});
@override
Widget build(BuildContext context) {
return FormBuilderTextField(
name: identifierForField,
);
}
}
我不明白的是,只有 1 个小部件使用密钥,那就是FormBuilder
小部件(我没有计算 'FormBuilderWrapper',因为它只是将密钥传递给FormBuilder
)
谁能指出我为什么会发生这种情况的正确方向?使用相同的“多个小部件”的解释GlobalKey
会很棒