我在从其他类的状态中调用函数时遇到问题。 使用globalkey无效,也不能在各自的班级中设置孩子和父母。 我正在使用最新版本的Flutter。
import 'package:flutter/material.dart';
import 'package:precisometrologia_app/globalslib.dart';
import 'package:precisometrologia_app/widgets/cert_idlogic.dart';
final certIDNumberState = new GlobalKey<CertIDNumberState>();
var certID = buildCertID();
class CertSubmitButton extends StatefulWidget {
CertSubmitButton({this.formKey});
var formKey;
int _certCounter = 0;
@override
_CertSubmitButtonState createState() =>
new _CertSubmitButtonState(formKey: formKey);
}
class _CertSubmitButtonState extends State<CertSubmitButton> {
_CertSubmitButtonState({this.formKey});
var formKey;
void _increment() {
setState(() {
widget._certCounter++;
certIncrementID = widget._certCounter;
certID = buildCertID();
});
}
@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(
color: Colors.green,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
onPressed: () {
if (formKey.currentState.validate()) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Certificado Enviado'),
backgroundColor: Colors.green,
));
_increment();
print(certID);
CertIDNumber.of(context).updateState();
}
},
child: Container(
margin:
EdgeInsets.only(left: 100.0, right: 100.0, top: 5.0, bottom: 5.0),
child: Text(
'Enviar Certificado',
style: TextStyle(
color: Colors.white,
),
)),
));
}
}
class CertIDNumber extends StatefulWidget {
static CertIDNumberState of(BuildContext context) => context.ancestorStateOfType(const TypeMatcher<CertIDNumberState>());
@override
CertIDNumberState createState() =>
new CertIDNumberState(key: certIDNumberState);
}
class CertIDNumberState extends State<CertIDNumber> {
CertIDNumberState({key: Key});
updateState() {
if (this.mounted){
setState(() {
certID = buildCertID();
});
}
}
@override
initState(){
updateState();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
margin:
EdgeInsets.only(top: 10.0, bottom: 5.0, left: 10.0, right: 10.0),
child: Column(children: <Widget>[
Card(
shape: BeveledRectangleBorder(),
child: Container(
margin: EdgeInsets.only(
bottom: 10.0, top: 10.0, left: 80.0, right: 80.0),
child: Text(('Certificado: ') + certID.toString()),
)),
Divider(),
]));
}
}
按下按钮后,调试控制台将返回此错误:
I / flutter(19413):引发了另一个异常:NoSuchMethodError: 方法'updateState'在null上调用。
我该怎么办?我在俯视什么吗?