When you work with Firebase Firestore in your Flutter app, you get data as documents. Each document has a unique ID that is separate from the data inside it.
The Need for ID in Model
Often, you want to have this document ID available directly within your data object after you read it. This makes it easy to refer to the original document later, for actions like updating or deleting.
Using json_serializable
The json_serializable
package is very helpful for converting data between JSON maps (like the data from Firestore) and your custom Dart objects. Here's how to make it handle the document ID in a special way.
Define the ID Field
First, add a field for the ID in your data model class. It should match the type of your document ID, usually a String
.
class MyDataModel {
final String? id;
final String name;
// ... other fields
}
Prevent Saving the ID
You don't want the document ID to be saved inside the document data when you write it back to Firestore. You can tell json_serializable
to exclude this field during serialization (when converting the object back to JSON). Use the @JsonKey
annotation with a custom toJson
function that always returns null
.
()
class MyDataModel {
(toJson: Converters.id, includeIfNull: false)
final String? id;
final String name;
// ... other fields
}
class Converters {
static String? id(String? id) => null; // This function ensures 'id' is not saved
}
The includeIfNull: false
part makes sure the field is completely left out of the JSON if the converter returns null.
Add ID When Reading
When you fetch a document, you get the document's data map and its ID separately. To put the ID into your model object, you need to manually add it to the data map before passing it to the fromJson
factory method generated by json_serializable
.
factory MyDataModel.fromJson(String id, Map<String, dynamic> json) =>
_$MyDataModelFromJson(json..['id'] = id);
By adding json..['id'] = id
, you include the document ID in the map just for the fromJson
process, allowing json_serializable
to populate the id
field in your new object.
This approach lets you easily work with the document ID inside your models fetched from Firebase without worrying about saving it back incorrectly.