我有一个带有3个标签栏的页面。在我的应用程序上,我有2种模式,即插入和编辑模式。在插入模式下,currentState通过表单验证绕过,但是在编辑模式下,我得到NoSuchMethodError:在null上调用getter'currentState'。我正在尝试拨打以下电话,因此我理解了这个问题:
List<dynamic> _treatment = form.fields['treatment'].currentState.value;
位于第二个标签中,而我位于第一个标签中。第三选项卡也会发生同样的情况。如果我转到第2个和第3个选项卡,然后单击“保存”按钮,则不会出错,因为创建了所有表单变量。因此,我的问题是如何绕过这种情况?当我是Flutter的新手时,我们将非常感谢您的帮助。我的应用程序的用户可以在任何选项卡上单击保存按钮。在编辑模式下,我无法阻止表单验证,因为它已在插入模式下完成。以下是我的保存部件代码:
Future _saveForm() async {
final form = _formKey.currentState;
if (form.validate()) {
//_formKey.currentState.save();
_validate = false;
print('saved');
form.save();
List<dynamic> _treatment = form.fields['treatment'].currentState.value;//this make problem as this is in tab 2
print(_treatment);
String treatment;
_treatment.forEach((dynamic treat) {
if (!treat.contains('FormBuilderFieldOption')) {
if (treatment == null) {
treatment = treat.toString();
} else {
treatment = treatment + ',' + treat.toString();
}
}
});
final int prefs = await Preference.getInt('userid');
final int _loginUser = prefs;
String _visithealthfacility =
form.fields['visithealthfacility'].currentState.value.toString();
if (_visithealthfacility == 'null') {
_visithealthfacility = null;
}
String _bloodinstool = form.fields['bloodinstool'].currentState.value;
if (_bloodinstool == null) {
_bloodinstool = 'No';
}
String _fever = form.fields['fever'].currentState.value;
if (_fever == null) {
_fever = 'No';
}
String _other = form.fields['other'].currentState.value;
if (_other == null) {
_other = 'Disoriented';
}
String _ocv = form.fields['ocv'].currentState.value;//this makes
problem as it's in tab 3
if (_ocv == null) {
_ocv = 'No';
}
final Country _country = form.fields['country'].currentState.value;
final District _district = form.fields['district'].currentState.value;
final Governorate _governorate =
form.fields['governorate'].currentState.value;
final HealthFacility _healthfacility =
form.fields['healthfacility'].currentState.value;
final ORT _ort = form.fields['ort'].currentState.value;
final Village _village = form.fields['village'].currentState.value;
final CTC _ctc = form.fields['ctc'].currentState.value;
final CaseData _caseData = CaseData(
id: widget.caseData.id,
age: form.fields['age'].currentState.value,
bloodinstool: _bloodinstool,
comment: form.fields['comment'].currentState.value,
countryId: _country.id,
ctcId: _ctc != null ? _ctc.id : null,
dehydration: form.fields['dehydration'].currentState.value,
diarrhoea: form.fields['diarrhoea'].currentState.value,
districtId: _district.id,
fever: _fever,
gender: form.fields['gender'].currentState.value,
governorateId: _governorate.id,
healthfacilityId: _healthfacility.id,
hospitalization: form.fields['hospitalization'].currentState.value,
month: form.fields['month'].currentState.value,
ocv: _ocv,
ortId: _ort != null ? _ort.id : null,
other: _other,
outcome: form.fields['outcome'].currentState.value,
stoolsample: form.fields['stoolsample'].currentState.value,
symptoms: form.fields['symptoms'].currentState.value.toString(),
treatment: treatment,
villageId: _village.id,
visithealthfacility: _visithealthfacility,
vomiting: form.fields['vomiting'].currentState.value,
username: _loginUser,
selected: 0,
);
print(_caseData.toString());
_caseDataBloc.updatecaseData(_caseData);
Navigator.pushNamed(context, '/caselist');
} else {
_validate = true;
FlushbarHelper.createError(
message: 'Mandatory fields are missing',
title: 'Please fill the mandatory fields.',
).show(context);
print('validation problem');
setState(() {
_autoValidate = true;
});
}
}
实际上,这与代码无关,但是我认为在选项卡中处理表单状态的技术。请参阅此gif文件Video showing the problem