Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLUTTER | How to add data to an existing document in firestore

I'm using firestore to store data of my flutter application, and I made a function that creates a document in firestore automatically after the user login My Firestore database

Now I want the user when he fills this form , the data will be added in the same document where the user's email exists.

[ RaisedButton(
              child: Text("Submit"),
              onPressed: () {
               final CollectionReference users = Firestore.instance.collection('users');
                Firestore.instance
                    .runTransaction((Transaction transaction) async {
                  CollectionReference reference =
                  Firestore.instance.collection('users');

                  await reference
                      .add({"fullname": nameController.text, "PhoneNumber": phoneController.text, "adresse": adressController.text});
                  nameController.clear();
                  phoneController.clear();
                  adressController.clear();

                });},][2]

I tried this code but it adds new document.

like image 517
STEP PLUS Avatar asked May 28 '18 04:05

STEP PLUS


People also ask

How do I update firestore documents?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.

How do I add data to a Subcollection in firestore?

The solution goes: Get the interested document (doc) and then: doc. ref. collection('Collection_Name'). add('Object_to_be_added') It is advisable to include a then/catch after the promise.


1 Answers

Specify document name before updating database.

Firestore.instance
  .collection('Products')
    .document('Apple')
      .updateData({
        'price': 120, 
        'quantity': 15
      });

Here my price and quantity data are numbers. If yours are Strings put String values there.

like image 157
Vinoth Kumar Avatar answered Sep 18 '22 08:09

Vinoth Kumar