Home > Software design >  How to write data in firebase cloud firestore with data type using flutter
How to write data in firebase cloud firestore with data type using flutter

Time:06-11

When saving data to the Firebase database I want every data to be saved according to its data type. But my all data is stored in String. How can I do it in flutter... like Amount will be int, Through will be String

[here is function image][1] here is my function

sandDataToDB() async {
CollectionReference _collectionReferance =
    FirebaseFirestore.instance.collection("Use-of-fund");
return _collectionReferance
    .doc()
    .set({
      "Details of Sector": _DetailsofSectorController.text,
      "Through": _ThroughController.text,
      "Amount": _AmountController.text,
      "Date": _DateController.text,
    })
    .then((value) => dialog())
    .catchError((error) => Fluttertoast.showToast(msg: "something wrong"));

}

here is all code

  customAdminTextField(
                          "Details of Sector", _DetailsofSectorController),
                      customAdminTextField("Through", _ThroughController),
                      customAdminTextField("Amount", _AmountController),
                      customAdminTextField("Date", _DateController),
                      const SizedBox(
                        height: 10,
                      ),
                      customSubmitButton("Submit ", () {
                        sandDataToDB();

                      })

CodePudding user response:

You have to parse your data. An example for an integer:

...    
"Amount": int.parse(_AmountController.text),
...

Here you can find the supported data types.

CodePudding user response:

According to your question, you might be using the controller in TextFormField, you can get the value of the TextFormField.

This widget covers a TextField widget in a FormField for convenience.

It is not necessary to have a Form ancestor. The Form merely simplifies the process of saving, resetting, or validating numerous fields at the same time. To use without a Form, supply a GlobalKey to the constructor and save or reset the form field with GlobalKey.currentState.

TextEditingController.text defines the initialValue when a controller is defined. A controller should be given if this FormField is part of a scrolling container that generates its children lazily, such as a ListView or a CustomScrollView. A stateful widget ancestor of the scrolling container should handle the controller's lifetime.

Example code:

TextFormField(
  decoration: const InputDecoration(
    icon: Icon(Icons.person),
    hintText: 'What do people call you?',
    labelText: 'Name *',
  ),
  onSaved: (String? value) {
    // This optional block of code can be used to run
    // code when the user saves the form.
  },
  validator: (String? value) {
    return (value != null && value.contains('@')) ? 'Do not use the @ char.' : null;
  },
)

You can also use the onSubmitted parameter in TextFormField. Like: onSubmitted: (String value)

Additionally, you can use something like this in the following code:

_formKey.currentState.save(); calls the onSaved() on each textFormField item, which gives all the fields a value and allows you to utilize them as needed.

  • Related