【发布时间】:2019-07-29 22:50:52
【问题描述】:
我遇到了一个问题,即我在以前的表单保存中使用的数据作为当前行保存在数据库中。例如,在第一次迭代中,当我使用“test1”作为名字并且第二次迭代与 test2 一起保存时,第二次迭代在数据库中保存为 test1 而不是 test2。 第一次迭代保存了“名字”的空数据。
我是飞镖/颤振的新手。
class InitialRegistrationForm extends StatefulWidget {
@override
_InitialRegistrationFormState createState() =>
_InitialRegistrationFormState();
}
class _InitialRegistrationFormState extends State<InitialRegistrationForm> {
final _formKey = GlobalKey<FormState>();
final _user = User();
bool _isLoading = false;
String msg = '';
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Builder(
builder: (context) => Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 5),
child: Text(
'BASIC DETAILS',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
decoration: TextDecoration.underline,
decorationColor: Colors.black87,
decorationStyle: TextDecorationStyle.wavy,
),
),
),
TextFormField(
decoration: InputDecoration(labelText: 'First name'),
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your first name';
}
},
onSaved: (val) => setState(() => _user.firstName = val),
),
Container(
padding: const EdgeInsets.symmetric(
vertical: 16.0, horizontal: 16.0),
child: RaisedButton(
onPressed: () {
_irfSubmit(
_user.firstName,
);
final form = _formKey.currentState;
print(form.toStringShort());
if (form.validate()) {
form.save();
_user.save();
}
},
child: Text('Save'),
),
),
],
),
),
),
);
}
}
_irfSubmit(String fn) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
try {
final http.Response response = await http.post(
"http://xxxxxxxxxxxx/public_html/include/participant_form_app.php",
body: {
"firstname": fn,
});
}
}
【问题讨论】: