[英]How to get value from TextFormField on Flutter
I would like to get input value from TextFormField.我想从 TextFormField 获取输入值。 And then I want to take it into Firebase.然后我想把它带入 Firebase。
If I have three TextFormField (last_name / first_name / birthday), how can I get these value and throw them into firebase?如果我有三个 TextFormField (last_name / first_name /birthday),我怎样才能得到这些值并将它们扔到 firebase 中?
You need to use a TextEditingController
for example:例如,您需要使用TextEditingController
:
TextEditingController lastNameController = TextEditingController();
TextEditingController firstNameController = TextEditingController();
TextEditingController birthdayController = TextEditingController();
Then inside the TextFormField
:然后在TextFormField
内:
child: TextFormField(
controller: firstNameController,
decoration: InputDecoration(
labelText: "Enter First Name",
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
// The validator receives the text that the user has entered.
validator: (value) {
if (value.isEmpty) {
return 'Enter last Name';
}
return null;
},
),
Create 3 fields, assign each controller
to each field.创建 3 个字段,将每个controller
分配给每个字段。 Then to access the value of each controller
, you need to use the text
property:然后要访问每个controller
的值,您需要使用text
属性:
DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("Users");
dbRef.child("id").set({
"firstName": firstNameController.text
})
Check this:检查这个:
https://flutter.dev/docs/cookbook/forms/retrieve-input https://flutter.dev/docs/cookbook/forms/retrieve-input
There are 2 ways depending on your use-case:根据您的用例,有两种方法:
TextField
widget has a callback method onChange
which you can use to get value once value is changed. TextField
小部件有一个回调方法onChange
,一旦值更改,您可以使用该方法获取值。
TextField
has a property controller
, to which you can asing TextEditingController
instance and use it later on where you need it (for example on a button click) like this textFieldController.text
which holds the current value of a textField TextField
有一个属性controller
,您可以将其作为TextEditingController
实例并稍后在需要它的地方(例如在单击按钮时)使用它,例如textFieldController.text
,它包含 textField 的当前值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.