Flutter - Create a Dart Model using Freezed Package
Last Updated :
28 Apr, 2025
In this article, we will learn how to create a dart model automatically with a sample of code using the freezed package. If you are making a large-scale app. You cannot write a model class file for every data you receive from the backend. So here's a simple solution for it where this model will automatically generate you just need to add sample data in some format.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the following packages
Add the following dev dependency in your project
Dart
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
build_runner: ^2.4.7
freezed: ^2.4.6
json_serializable: ^6.7.1
Add the follwing dependency in your pubspec.yaml file
Dart
dependencies:
flutter:
sdk: flutter
freezed_annotation: ^2.4.1
json_annotation: ^4.8.1
Let's understand what this package will use for
- freezed_annotation: This package is used to give a sample file access freezed keyword to create new model file
- json_annotation: This create code for JSON serialization and deserialization.
- build_runner: This package provides a concrete way of generating files using Dart code.
- freezed: This create model class and functions
- json_serializable: To generate to/from JSON code for a class, annotate it with JsonSerializable
Step 3: Create a model folder and create a user model file in it and add sample data in it
We are using this sample json data for which we will create a model
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
}
Sample Code for creating freezed model in models/postmodels/postmodel.dart.
Dart
// ignore_for_file: non_constant_identifier_names
import 'package:freezed_annotation/freezed_annotation.dart';
// name will be same as your current file
// name like we have given postmodel.dart
part 'postmodel.freezed.dart';
part 'postmodel.g.dart';
// This is used to create a freezed model
@freezed
class PostModel with _$PostModel {
const factory PostModel({
// Sample key from json to show how
// we recieve the data from backend
required int id,
required int userId,
required String title,
required String body,
}) = _PostModel;
factory PostModel.fromJson(Map<String, dynamic> json) =>
_$PostModelFromJson(json);
}
Step 4: Run command to create automatic freezed model class in the model
In terminal run this command to create a model class
Dart
flutter pub run build_runner build --delete-conflicting-outputs
Before running this command
After running this command 2 more files will be created i.e post_model.freezed.dart,post_model.g.dart.

This file contains many function like copyWith, toJson, from Json, toString, hashcode and many more. This will help you to access the model data easily.
Similar Reads
Flutter - Creating Time Picker Using Material Design Time Picker is a UI component that controls or lets the selection of time in either 24-hours format or AM/PM format. Mainly is job is to ease out the process of time selection and to ensure that the user is picking or selecting a valid time in the application. This can be used in the applications su
7 min read
Flutter - Creating Snackbar Using GetX Library Sometimes, it's very useful to show the message when a certain action takes place in our app. Let's suppose, we have a list of items and we want to delete any item from the list then after deleting those items, some message should be displayed to inform the user that the item has been deleted. If we
2 min read
Flutter - Onboarding Screen Using flutter_overboard Package In Flutter, Onboarding screens, also known as welcome screens or introduction screens, are a collection of screens or pages that are displayed to users when they first launch a mobile app or a software application. The primary purpose of onboarding screens is to introduce users to the key features,
5 min read
How to Get Coordinates Widgets using rect_getter Package in Flutter? Sometimes we need to get a widget's location or show some overlay widget or something similar over a widget whose location on the screen could be dynamic. In this article, we are going to discuss the way to get coordinates of a widget using the rect_getter package. Follow the steps below : 1. Create
9 min read
Flutter - Creating Dialog in using GetX Library When we want to show anything in the form of the dialog then we can create this Dialog using the GetX library in Flutter. When we normally create a dialog in flutter then it uses context and builder to create a Dialog. This is not a good practice for a developer to create Dialogs using contexts and
3 min read
Flutter - Convert JSON to Model Class in Dart Many times you are receiving API data but it is difficult to create a model class manually for some data that are too big. So we can make a model with just 2 steps. Let's take sample data for which we want to create a model class. { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": f
2 min read
Simple Calculator App using Flutter Flutter SDK is an open-source software development kit for building beautiful UI that is natively compiled. In this article, we will build a Simple Calculator App that can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input. Making
5 min read
Flutter - Store User Details Using Firebase Firebase is a product of Google that helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way. No programming is required on the Firebase side which makes it easy to use its features more efficiently. It provides services
3 min read
Upload and Retrieve Images on MongoDB using Dart in Flutter To upload files from a local system to the dedicated server is called file uploading and retrieval files from the dedicated server to a local system is called file retrieving. Â It works exactly the same as the definition when we select a file from the Android device and click the submit button, the
6 min read
How to Change Package Name in Flutter? Every Flutter app has a package name that uniquely identifies your app on the Google Play Store and Apple App Store. In this article, we will learn how to change the package name with a simple and easy method. The package name is basically a unique identity to identify that app on App Store, Play St
2 min read