Im pretty new to Flutter, which is why I seek some help here. I would like to push/add more exercises to the same document id in firestore. So it is stored as one workout. I have made a class called exercise, I would like to ad more under. I have included a picture of the firestore.
This is the following way I add to firestore:
final exercise = Exercise(
date: DateTime.now(),
name: 'Leg',
reps: ['1', '2'],
weight: ['50', '40']);
String id = FirebaseFirestore.instance.collection('workouts').doc().id;
FirebaseFirestore.instance
.collection('workouts')
.doc(id)
.set(exercise.toFirestore());
With little information, if you want to add to the array, for example increase the list of reps (From ['1','2'] to this ['1','2','3'], you can do this:
String id = FirebaseFirestore.instance.collection('workouts').doc().id;
FirebaseFirestore.instance
.collection('workouts')
.doc(id)
.update({'refs': Fielvalue.arrayUnion(['3'])});
But if you want to include fields to an already made document:
var db = FirebaseFirestore.instance;
final batch = db.batch();
var workout = db.collection("workouts").doc("your document");
//update
batch.update(workout, {"situps": ['1','2','3','4']});
// Commit the batch
batch.commit().then((_) {
// done
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.