Код ниже работает нормально, я нажимаю плоскую кнопку и всплывающее окно с предупреждением.
Вопросы:
1) Но если я добавлю form.save, он показывает ошибку
flutter: следующая ошибка NoSuchMethodError была выдана при обработке
жест: flutter: сеттер 'clientName =' был вызван на нуль.
флаттер: получатель: ноль флаттер: пробный вызов: clientName = "423"
2) Как получить значение ввода текста, например us: clientName..etc. Я всегда получаю ноль. Пожалуйста, помогите.
class _formState extends State<form> {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
Booking booking;
@override
Widget build(BuildContext context) {
final getCourse = widget.course;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
//title: Text('${widget.course.title}\n地址: ${widget.course.address}\nTEL: ${widget.course.tel}'),
title: Text('即日訂位'),
),
body: Container(
child: Form(
key: formKey,
child: ListView(
children: <Widget>[
ListTile(
leading: Icon(Icons.person_outline),
title: TextFormField(
initialValue: "",
onSaved: (val) => booking.clientName = val,
validator: (val) => val == "" ? val : null,
decoration: new InputDecoration(
labelText: "Name",
),
),
),
//Send or Post button
FlatButton(
textColor: Colors.blueAccent,
child: Text("OK"),
color: Colors.transparent,
onPressed: () {
_showFormDialog();
SystemChannels.textInput.invokeMethod('TextInput.hide');
},
)
],
),
),
),
);
}
void _showFormDialog() {
final FormState form = formKey.currentState;
if (form.validate()) {
//form.save();
var alert = new AlertDialog(
content: new Row(
children: <Widget>[
Text("hihi")
],
),
actions: <Widget>[
new FlatButton(
onPressed: () {
form.reset();
Navigator.pop(context);
},
child: Text("ok")),
new FlatButton(onPressed: () => Navigator.pop(context),
child: Text("cancel"))
],
);
showDialog(context: context,
builder: (_) {
return alert;
});
}
else {
var alert = new AlertDialog(
content:
new Row(
children: <Widget>[
Text("error!")
],
),
actions: <Widget>[
new FlatButton(onPressed: () => Navigator.pop(context),
child: Text("OK"))
],
);
showDialog(context: context,
builder: (_) {
return alert;
});
}
}
}