Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Streamline your Flutter models with Freezed

Dart is fantastic, but let’s face it: defining a “model” might be difficult at times. Consider this:

  1. You must define properties and constructors.
  2. Override operator ==, hashCode, and toString.
  3. To clone the object, use the copyWith method.
  4. Take care of the (de)serialization.

That boilerplate code is a lot! Hundreds of lines can result from all of this, which raises the possibility of errors and makes your code more difficult to comprehend.

In this article, we will be explore Streamline your Flutter models with Freezed. We will learn how to execute a demo program. We will show you how to use freezed in Flutter with the freezed package in your Flutter applications.

For Freezed:

freezed | Dart package
Code generation for immutable classes that have a simple syntax/API without compromising on the features.pub.dev

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

Table Of Contents:

Introduction

Implementation

Code Implement

Conclusion



Introduction:

Freezed is a potent Dart code creation tool that:

  1. creates constructors, equality checks, toString, and other things automatically.
  2. simplifies null safety and immutability for your models.
  3. simplifies (de)serialization.

Freezed handles the rest, allowing you to concentrate on specifying the structure of your model.

Implementation:

Step 1: Add the dependencies

Include build_runner and bdd_widget_test in your pubspec.yaml file’s dev_dependencies section:

dependencies:
  freezed_annotation: latest version
  json_annotation: latest version

dev_dependencies:
  build_runner: latest version
  freezed: latest version
  json_serializable: latest version

Step 2: Run flutter packages get in the root directory of your app

flutter pub get

How to implement code in your file :

You need to implement it in your code respectively:

Converting a Post Model to Freezed

As an example, let’s look at a post model. The conventional definition is as follows:

class Post {
final int id;
final String title;
final String body;

Post({required this.id, required this.title, required this.body});

factory Post.fromJson(Map<String, dynamic> json) {
return Post(
id: json['id'],
title: json['title'],
body: json['body'],
);
}
}

Let’s use Freezed to rewrite it now. Make a new post.dart file and specify your model:

import ‘package:freezed_annotation/freezed_annotation.dart’;

part ‘post.freezed.dart’;
part ‘post.g.dart’;

@freezed
class Post with _$Post {
const factory Post({
required int id,
required String title,
required String body,
}) = _Post;

factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);
}

This is what’s taking place:

  1. It instructs the package to create the boilerplate for the Post class using the annotation @freezed.
  2. The const factory is used to define the constructor.
  3. A fromJson method is created to handle serialisation.

Run the code generator:

dart run build_runner build

The following files will be produced as a result:

  • post.freezed.dart: The created constructors, equality, copyWith, and other utilities are contained.
  • post.g.dart: Serialisation logic is contained.

Using the Generated Code

You can use the Post model as follows, as Freezed has already done all the hard work for you:

Create a Post Instance:

final post = Post(id: 1, title: 'Hello World', body: 'This is the body of the post.');

Clone an Object with Modifications:

final updatedPost = post.copyWith(title: 'Updated Title');

Serialize to JSON:

final json = post.toJson();

Deserialize from JSON:

final newPost = Post.fromJson({
  'id': 2,
  'title': 'New Post',
  'body': 'This is a new post body.',
});

Conclusion:

In the article, I have explained how the Streamline your Flutter models with Freezed; you can modify this code according to your choice. This was a small introduction to Streamline your Flutter models with Freezed User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on trying the Streamline your Flutter models with Freezed in your Flutter projectsWe will show you what the Introduction is. The agony of defining models in Dart is eliminated by Freezed. It takes care of the boilerplate so you can concentrate on creating amazing apps rather than being distracted by repeating code. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I need to correct something? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.