我正在尝试使用GlobalKeys来维护两个选项卡之间的文本信息,但是奇怪的是,GlobalKey的 currentState 显示为空,最终无法访问变量的数量。
我有两个使用键并访问其值的小部件。
第一个小部件:
class FirstWidget extends StatefulWidget {
GlobalKey<_CounterState> formState;
FirstWidget({Key key, this.formState}) : super(key: key);
@override
_FirstWidgetState createState() => _FirstWidgetState();
}
class _FirstWidgetState extends State<FirstWidget> {
@override
Widget build(BuildContext context) {
String value = widget.formState.currentState == null
? "empty"
: widget.formState.currentState.toString();
return Container(
child: new Text("First Count : " + value),
);
}
}
第二个小部件:
class SecondWidget extends StatefulWidget {
GlobalKey<_CounterState> formState;
SecondWidget({Key key, this.formState}) : super(key: key);
@override
_SecondWidgetState createState() => _SecondWidgetState();
}
class _SecondWidgetState extends State<SecondWidget> {
@override
Widget build(BuildContext context) {
String value = widget.formState.currentState == null
? "empty"
: widget.formState.currentState.toString();
return Container(
child: new Text("Second Count : " + value),
);
}
}
我正在传递全局密钥,例如:
new FirstWidget(formState: _globalFormKey),
new SecondWidget(formState: _globalFormKey)