简体   繁体   English

如何从 Flutter 上的 TextFormField 获取值

[英]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 中?

在此处输入图像描述

  • I had already connect and get values from firebase.我已经从 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:根据您的用例,有两种方法:

  1. TextField widget has a callback method onChange which you can use to get value once value is changed. TextField小部件有一个回调方法onChange ,一旦值更改,您可以使用该方法获取值。

  2. 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.

相关问题 来自 Future (Firebase) 的 TextFormField (Flutter) 中的初始值 - Initial value in a TextFormField (Flutter) coming from a Future (Firebase) 我如何将数据从flift中的textformfield中保存为Cloud Firebase的数字 - how can I save data to cloud firebase as number from textformfield in flutter 如何将未来的字符串设置为 TextFormField 的“初始值” - How to set future String to 'initial value' of TextFormField 如何在 Cloud FireStore 的 TextFormField 中提供 initialValue - how to provide initialValue in TextFormField from Cloud FireStore 如何使用 Flutter 从带有“文档 ID”的 firestore 获取指定值 - How to get specified value from firestore with `DOCUMENT ID` with Flutter 如何从 stream 获取更新 Flutter DropdownButton 中的值? - How to get Value to update in Flutter DropdownButton from stream? 如何从 firebase 获取值并将其放入 Flutter 中的文本中 - How can I get a value from firebase and and put it in a text in Flutter 如何根据字段值从 flutter 中的 Firestore 获取文档列表 - How to get list of document from firestore in flutter based on field value Flutter 使用 firebase 数据库进行文本字段验证 - Flutter textformfield validation with firebase database 如果在 TextFormField 中未输入值,如何将字段另存为“null”到 Firebase - How to save a field as 'null' to Firebase if no value is entered in TextFormField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM