Reputation: 2443
I would like to get input value from TextFormField. And then I want to take it into Firebase.
If I have three TextFormField (last_name / first_name / birthday), how can I get these value and throw them into firebase?
Upvotes: 15
Views: 41099
Reputation: 1588
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
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
Upvotes: 11
Reputation: 80924
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
Upvotes: 48