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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Use Case Diagram - Unified Modeling Language (UML) A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read
Half Wave Rectifier A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th
15 min read
Flutter Tutorial This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
What is Agile Methodology? The Agile methodology is a proper way of managing the project with breaking them into smaller phases which is iteration. It basically focus on flexibility of the project which we can change and improve the team work regularly as per requirements.Table of ContentWhat is Agile?What is the Agile Method
14 min read
How to Download and Install the Google Play Store The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed?In this step-by-step guide, youâll learn how to safely download and install
6 min read
Top 8 Software Development Life Cycle (SDLC) Models used in Industry Software development models are various processes or methods that are chosen for project development depending on the objectives and goals of the project. Many development life cycle models have been developed to achieve various essential objectives. Models specify the various steps of the process a
9 min read
IEEE 802.11 Architecture The IEEE 802.11 standard, commonly known as Wi-Fi, outlines the architecture and defines the MAC and physical layer specifications for wireless LANs (WLANs). Wi-Fi uses high-frequency radio waves instead of cables for connecting the devices in LAN. Given the mobility of WLAN nodes, they can move unr
9 min read
Transaction in DBMS In a Database Management System (DBMS), a transaction is a sequence of operations performed as a single logical unit of work. These operations may involve reading, writing, updating, or deleting data in the database. A transaction is considered complete only if all its operations are successfully ex
10 min read