Update method from withReference document should be same as set method #6223
-
Currently, the document from the withReference collection can set with the object: 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,
};
}
}
final moviesRef = FirebaseFirestore.instance.collection('movies').withConverter(
fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
toFirestore: (movie, _) => movie.toJson(),
);
final id = '1';
final movie = Movie(title: 'Star Wars: A New Hope (Episode IV)', genre: 'Sci-fi');
await moviesRef.doc(id).set(movie); However, when updating the doc, it requires a Map<String, dynamic>: await moviesRef.doc(id).update(movie.toJson()); I expected it to pass an object instead: await moviesRef.doc(id).update(movie); What is the reason for the different implementation for set and update? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
moviesRef.doc(id).update({ 'title': 'New title' }); This is not feasible if |
Beta Was this translation helpful? Give feedback.
-
That is true since set is for whole object update/create while update is for partial updates, right? |
Beta Was this translation helpful? Give feedback.
update
is for partial object update. More specifically, with update, we may want just change the title:This is not feasible if
update
receives a class as a parameter. If it did, that would makeupdate
identical toset
.