skip to Main Content

Im reading the firestore flutter sdk documentation. It shows how to use a serializable class to automatically convert documents to objects with this example:

class Movie {
  Movie({required this.title, required this.genre});

  Movie.fromJson(Map<String, Object?> json)
    : this(
        title: json['title']! as String,
        genre: json['genre']! as String,
      );

  final String title;
  final String genre;

  Map<String, Object?> toJson() {
    return {
      'title': title,
      'genre': genre,
    };
  }
}

and then create a new document using the add function:

 await moviesRef.add(
    Movie(
      title: 'Star Wars: A New Hope (Episode IV)',
      genre: 'Sci-fi'
    ),
  );

How would I get the documentId after adding a new document or when querying?

2

Answers


  1. I tried to achieve a behavior like this before, in Firebase Firestore, either you call moviesRef.add() then firebase will add a new document with a completely auto-generated uid, or if you’re having a specific uid then you can just use the moviesRef.doc(hereTheId).set().

    However , the moviesRef.add() returns a DocumentReference which you can get from it the is like this example:

    using then:

    collection('students').add(/*...*/)
    

    .then((val)=> print(val.documentID)});

    using await/async:

    final newDocument = await moviesRef.add(/*...*/);
    print(newDocument.documentID);
    

    You can also get a completely auto-generated uid before sending the request , then call moviesRef.doc(hereTheId).set() like this:

        final String id  = moviesRef.doc().id;
        moviesRef.doc(id).set(/*...*/)`
    

    Hope this helps.

    Login or Signup to reply.
  2. I think we also need to manipulate the Ref to be able to use like you mentioned above with add method, which will get the Future<DocumentReference> add(Movie data) Returns a DocumentReference with an auto-generated ID, after populating it with provided data.

    As shown in here like following:

    final moviesRef = FirebaseFirestore.instance.collection('movies').withConverter<Movie>(
          fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
          toFirestore: (movie, _) => movie.toJson(),
        );
    
    

    And while adding it you need added document id you can achieve it like this :

    final moviesRef =
          FirebaseFirestore.instance.collection('movies').withConverter<Movie>(
                fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
                toFirestore: (movie, _) => movie.toJson(),
              );
    
      // Add a movie
      DocumentReference docreference = await moviesRef.add(
        Movie(title: 'Star Wars: A New Hope (Episode IV)', genre: 'Sci-fi'),
      );
    
      String id = docreference.id;
    
      debugPrint('documentId: $id'); // will give you the added Document documentId.
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search