How to get value from TextFormField on Flutter
17,658
Solution 1
You need to use a TextEditingController
for example:
TextEditingController lastNameController = TextEditingController();
TextEditingController firstNameController = TextEditingController();
TextEditingController birthdayController = TextEditingController();
Then inside the 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. Then to access the value of each controller
, you need to use the text
property:
DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("Users");
dbRef.child("id").set({
"firstName": firstNameController.text
})
Check this:
https://flutter.dev/docs/cookbook/forms/retrieve-input
Solution 2
There are 2 ways depending on your use-case:
TextField
widget has a callback methodonChange
which you can use to get value once value is changed.TextField
has a propertycontroller
, to which you can asingTextEditingController
instance and use it later on where you need it (for example on a button click) like thistextFieldController.text
which holds the current value of a textField
Author by