[英]Pushing more data to the same document id
Im pretty new to Flutter, which is why I seek some help here.我对 Flutter 很陌生,这就是我在这里寻求帮助的原因。 I would like to push/add more exercises to the same document id in firestore.
我想向 Firestore 中的同一文档 ID 推送/添加更多练习。 So it is stored as one workout.
所以它被存储为一个锻炼。 I have made a class called exercise, I would like to ad more under.
我做了一个class叫做练习,想多下广告。 I have included a picture of the firestore.
我附上了一张火炉的照片。
This is the following way I add to firestore:这是我添加到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:几乎没有信息,如果你想添加到数组中,例如增加代表列表(从['1','2']到这个['1','2','3'],你可以做这个:
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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.