Gabe
Gabe

Reputation: 6865

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

Upvotes: 0

Views: 230

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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,
      });

Upvotes: 1

Related Questions