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

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
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();
});}
I tried this code but it adds new document.

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.

Best practice is to use transaction.
Make sure that document reference is a reference to a file that you wish to update.
Firestore.instance.runTransaction((transaction) async {
await transaction.update(
documentReference, data);
};
It will make sure that update happens in order in case there are many clients doing it.
In the case of a concurrent edit, Cloud Firestore runs the entire transaction again. For example, if a transaction reads documents and another client modifies any of those documents, Cloud Firestore retries the transaction. This feature ensures that the transaction runs on up-to-date and consistent data.
More info here

Try .setData({"fullname": nameController.text, "PhoneNumber": phoneController.text, "adresse": adressController.text}, merge: true).

Update 2021:
You need to update the data to add it to an existing document.
var collection = FirebaseFirestore.instance.collection('users');
collection
.doc('doc_id') // <-- Doc ID where data should be updated.
.update({'age' : 20}) // <-- New data
.then((_) => print('Updated'))
.catchError((error) => print('Update failed: $error'));

Related

Flutter firestore add collection and data in document

I want to add data and collection both I am trying like this
Future<void> createRoom(collection, docid, data) async {
await FirebaseFirestore.instance
.collection(collection)
.doc()
.set(data)
.collection('messages');
}
But its showing error The method 'collection' isn't defined for the type 'Future'.
If you are trying to set a document in a sub-collection then first create a DocumentReference to that collection and then use set():
await FirebaseFirestore.instance
.collection(collection)
.doc("doc_id")
.collection('messages')
.doc("msg_id")
.set(data)
Do note that you should pass document ID in doc() else it'll generate a random ID every time instead updating existing one.
I want to add data and collection both
You don't need to create a collection explicitly if you are not adding a document yet. It'll be created at the time you add first document in collection. So try refactoring the code as shown below:
Future<void> createRoom(collection, docid, data) async {
await FirebaseFirestore.instance
.collection(collection)
.doc("doc_Id")
.set(data);
// simply add a document in messages sub-collection when needed.
}
Also checkout: How to create collection in Cloud Firestore from Javascript?

Deleting docs in cloud Firestore

I'm trying to code a todo App linked with cloud firestore. I want that when i tap on my tile which is a Gesture Detector, the task is deleted on firestore but it won't work.
Here's my code for my Function i use and all things that are necessary.
final _firestore = FirebaseFirestore.instance;
///
void deleteTask(){
_firestore.collection("todos").doc().delete();
}
///
child: GestureDetector(
onLongPress: (){
deleteTask();
},
child: Container(
When you are setting new doc Firebase probably this kind of code you use:
var db = FirebaseFirestore.instance;
DocumentReference ref = db.collection('tasks').doc();
ref.set(
{'field': data},
);
// save your ref.id here in order to let user remove the task eventually
This piece of code you wrote just puts a new document on a field. Now when you create or populate your Task class, you need to obtain document ID in order to delete it.
removeTask(String documentID) async {
var db = FirebaseFirestore.instance;
DocumentReference ref = db.collection('tasks').doc(documentID);
ref.delete();
}
// ref.id gives you the doc ID. Put it in your Task class when fetching docs
// so if user wants to delete any document, he/she can delete it.

New document being created instead of updated

I have an onPress on an edit page - it should update the document when edited and saved.
However, it is currently creating a new document with that data instead.
onPressed: () async {
//controllers...
await updateContact(context);
Navigator.pop(context, widget.contact);
}
.
final db = FirebaseFirestore.instance;
.
Future updateContact(context) async {
final uid = await TheProvider.of(context).auth.getCurrentUID();
await db
.collection('userData')
.doc(uid)
.collection('Contacts')
.doc(widget.contact.documentId)
.set({
'Name': oneController.text,
'PhoneNumber': int.tryParse(twoController.text),
'Location': threeController.text,
'Rating': int.tryParse(fourController.text),
'Instagram': fiveController.text,
'Birthday': int.tryParse(sixController.text),
'Notes': sevenController.text},
SetOptions(merge: true));
.
Contact.fromSnapshot(DocumentSnapshot snapshot) :
//...
documentId = snapshot.id;
I am not sure how to best resolve this.
Yes, using set() will override all the data already present in firestore.
and yes, using update is the way to go, but keep in mind not to call .toJson() on the entire object as update only takes the fields that are needed to be updated.
So if you update with the entire object, it'll create a new one again.
You could pass it like this
.update({'name': oneController.text, 'birth': twoContorller.text, 'email': threeController.text});
alternatively, you can also use set( setOptions: SetOptions(merge:true)) this will update only the fields that have changed in the document.

How to update collection documents in firebase in flutter?

I want to update a document field and I've tried the following code but it doesn't update.
can anyone give me a solution, please?
My Code:
var snapshots = _firestore
.collection('profile')
.document(currentUserID)
.collection('posts')
.snapshots();
await snapshots.forEach((snapshot) async {
List<DocumentSnapshot> documents = snapshot.documents;
for (var document in documents) {
await document.data.update(
'writer',
(name) {
name = this.name;
return name;
},
);
print(document.data['writer']);
//it prints the updated data here but when i look to firebase database
//nothing updates !
}
});
For cases like this, I always recommend following the exact types in the documentation, to see what options are available. For example, a DocumentSnapshot object's data property is a Map<String, dynamic>. To when you call update() on that, you're just updating an in-memory representation of the document, and not actually updating the data in the database.
To update the document in the database, you need to call the DocumentReference.updateData method. And to get from the DocumentSnapshot to a DocumentReference, you call the DocumentSnapshot.reference property.
So something like:
document.reference.updateData(<String, dynamic>{
name: this.name
});
Unrelated to this, your code looks a bit non-idiomatic. I'd recommend using getDocuments instead of snapshots(), as the latter will likely result in an endless loop.
var snapshots = _firestore
.collection('profile')
.document(currentUserID)
.collection('posts')
.getDocuments();
await snapshots.forEach((document) async {
document.reference.updateData(<String, dynamic>{
name: this.name
});
})
The difference here is that getDocuments() reads the data once, and returns it, while snapshots() will start observing the documents, and pass them to us whenever there's a change (including when you update the name).
Update 2021:
Lot of things have changed in the API, for example, Firestore is replaced by FirebaseFirestore, doc is in, etc.
Update a document
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('some_id') // <-- Doc ID where data should be updated.
.update({'key' : 'value'}) // <-- Updated data
.then((_) => print('Updated'))
.catchError((error) => print('Update failed: $error'));
Update nested value in a document:
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('some_id') // <-- Doc ID where data should be updated.
.update({'key.foo.bar' : 'nested_value'}) // <-- Nested value
.then((_) => print('Updated'))
.catchError((error) => print('Update failed: $error'));
To update some fields of a document without overwriting the entire document, use the following language-specific update() methods:
final washingtonRef = FirebaseFirestore.instance.collection("cites").doc("DC");
washingtonRef.update({"capital": true}).then(
(value) => print("DocumentSnapshot successfully updated!"),
onError: (e) => print("Error updating document $e"));
Server Timestamp
You can set a field in your document to a server timestamp which tracks when the server receives the update.
final docRef = FirebaseFirestore.instance.collection("objects").doc("some-id");
final updates = <String, dynamic>{
"timestamp": FieldValue.serverTimestamp(),
};
docRef.update(updates).then(
(value) => print("DocumentSnapshot successfully updated!"),
onError: (e) => print("Error updating document $e"));
Update fields in nested objects
If your document contains nested objects, you can use "dot notation" to reference nested fields within the document when you call update():
// Assume the document contains:
// {
// name: "Frank",
// favorites: { food: "Pizza", color: "Blue", subject: "recess" }
// age: 12
// }
FirebaseFirestore.instance
.collection("users")
.doc("frank")
.update({"age": 13, "favorites.color": "Red"});
Update elements in an array
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.
final washingtonRef = FirebaseFirestore.instance.collection("cities").doc("DC");
// Atomically add a new region to the "regions" array field.
washingtonRef.update({
"regions": FieldValue.arrayUnion(["greater_virginia"]),
});
// Atomically remove a region from the "regions" array field.
washingtonRef.update({
"regions": FieldValue.arrayRemove(["east_coast"]),
});
Increment a numeric value
You can increment or decrement a numeric field value as shown in the following example. An increment operation increases or decreases the current value of a field by the given amount.
var washingtonRef = FirebaseFirestore.instance.collection('cities').doc('DC');
// Atomically increment the population of the city by 50.
washingtonRef.update(
{"population": FieldValue.increment(50)},
);

How to add Collection To Document Firestore

I have a User collection in my Firstore database, and I want to imbed a Workout collection in each document of my User collection. I can do this on the Cloud Firestore Dashboard, but how can I do this when I create a new User document in my Flutter code?
My normal code for adding a user looks like this:
Firestore.instance.runTransaction((transaction) async {
await transaction
.set(Firestore.instance.collection('users').document(uuid_), {
'grade': _grade,
'name': _name,
});
});
but I want to somehow add a Workout subcollection to my newly created User document. Thanks for any help
There is no public API to create a new subcollection. Instead a collection is created automatically when you add the first document to it.
So something like:
transaction.set(
Firestore.instance.
collection('users').document(uuid_).
collection("workouts").document("workoutid"), {
'grade': _grade,
'name': _name,
});

Resources