Ad
Firestore: How To Store Data Back Again In The Right Documents After Fetching Data From Multiple Documents
In my App a user can track his workouts, which I want to save in cloud firestore. My idea is to store a list of workouts for each month to prevent that a document gets too big. So a document would look something like this:
month: '2022-02',
workouts: [
{
date: '2022-02-01',
exercises: [
{
sets: [{'reps': 12, 'weight': 80}
{'reps': 12, 'weight': 80}
],
},
],
},
{
date: '2022-02-02',
exercises: [
{
sets: [{'reps': 10, 'weight': 90}
{'reps': 10, 'weight': 90}
],
},
],
},
],
Question:
When I fetch for example the workouts from the last three month and put them in a list to let the user interact with these workouts. How can I store them again in cloud firestore sorted in the right month?
Ad
Answer
To be able to write data back to the same document you read from, you will need to track the document ID for each piece of data in your list view, in addition to the fields you read from the document itself.
When you have the document ID, writing data to it is as easy as:
FirebaseFirestore.instance
.collection("yourWorkoutsCollection")
.document(documentIdForTheMonth)
.update({ 'workouts': FieldValue.arrayUnion([theWorkoutToAdd]) });
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad