FieldValue.increment for Cloud Firestore in Flutter
This has been added to the cloud_firestore
Flutter plugin as of version 0.10.0
.
You can use FieldValue.increment
with a double
(e.g. FieldValue.increment(1.0)
) or an int
(e.g. FieldValue.increment(2)
).
Here is the relevant pull request.
If anyone reads this before the version is pushed to Pub: The link to the version will not yet work, but in the meantime you can use this Git commit, which contains a fix I did.
As far as I can see in the change log for FlutterFire, the increment operator has not been added yet.
I added a feature request to the repo.
User that Import:
import 'package:cloud_firestore/cloud_firestore.dart';
For Increment:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(1)});
For Decrement:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(-1)});