Firebase document as entity model

Flutter tips Published on

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.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why put the document ID in the model?

It makes it convenient to access the ID of a document directly from its corresponding object when you need to perform actions like updating or deleting that specific document.

How does this stop the ID from being saved back?

We use a special setting (`@JsonKey(toJson: ...)` and a converter function) that tells the serialization process to output `null` for the ID field. Since we also set `includeIfNull: false`, the field is completely skipped when creating the JSON to save.

What is `json_serializable`?

It's a package in Dart/Flutter that automates the process of converting your custom Dart objects to and from JSON data, saving you from writing this code manually.

Where does the ID come from when reading?

When you read a document from Firebase, the document ID is given to you separately from the document's data map. You then pass both the ID and the data map to your model's `fromJson` factory method and manually add the ID to the map before processing it.

Read more
You may also be interested in
Benchmark a function within a test  blog card image
Benchmark a function within a test
Published on 2025-05-12T12:24:19.613Z
Published on
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved