Taming Tedium with Flutter Freeze: Effortless Code Generation for Your Flutter Apps

Affan Minhas
Blocship
Published in
3 min readMar 3, 2024
Flutter Freeze

In the ever-evolving world of Flutter development, efficiency is paramount. While the framework itself empowers us to create stunning and interactive apps, writing boilerplate code can bog down our progress. But fear not, Flutter developers, for a valiant hero stands ready to combat this tedium: Flutter Freeze!

What is Flutter Freeze?

Flutter Freeze is a powerful package that utilizes code generation to automate the creation of essential methods for your data classes, saving you precious time and effort.

Why Use Flutter Freeze?

  • Say goodbye to manually writing copyWith, toString, ==, and hashCode methods. Freeze handles these for you, keeping your code clean and concise.
  • With less boilerplate, your code becomes more readable and understandable, both for you and your fellow developers.
  • Freeze facilitates seamless serialization (conversion to/from JSON), making data persistence and communication a breeze.

Getting Started with Flutter Freeze

  • Installation: Add the freezed_annotation and build_runner packages to your pubspec.yaml file:
dependencies:
freezed_annotation: ^2.1.2
dev_dependencies:
build_runner: ^4.3.1
freezed: ^2.2.1
  • Generate Code: Run the following command in your terminal to initiate code generation:
flutter pub run build_runner watch

Creating Your First Data Class

  • Import the Package: In your Dart file, import the freezed_annotation package:
import 'package:freezed_annotation/freezed_annotation.dart';
  • Annotate Your Class: Use the @freezed annotation to mark your class for code generation:
@freezed
abstract class UserModel with _$UserModel {
const factory UserModel({
required String firstName,
required String lastName,
required String email,
}) = _UserModel;
}

That’s it! Now, running flutter pub run build_runner watch will generate a separate file (user_model.g.dart) containing the following methods:

  • copyWith: Allows you to create a new instance of the class with modified properties, keeping the original intact.
  • toString: Returns a string representation of the object for debugging and logging purposes.
  • == and hashCode: Enable comparison and hashing of objects, essential for equality checks and efficient data storage.

Example Usage

UserModel user = UserModel(
firstName: 'Affan',
lastName: 'Minhas',
email: 'affan.minhas@example.com',
);

// Create a new user with an updated email
UserModel updatedUser = user.copyWith(email: 'affansultan@example.com');

print(user.toString()); // Prints 'UserModel(firstName: John, lastName: Doe, email: john.doe@example.com)'

if (user == updatedUser) {
print('Objects are equal'); // This won't be printed because the emails differ
}

Beyond the Basics

Flutter Freeze offers several additional features to enhance your data classes further:

  • Unions and Sealed Classes: Model complex data structures like login states or error types using unions or sealed classes.
  • Custom Serialization: Control the serialization process with custom toJson and fromJson methods.

Conclusion

By embracing Flutter Freeze, you can streamline your development process, reduce boilerplate code, and craft cleaner, more readable, and maintainable Flutter apps. So, ditch the tedium and embrace the power of code generation!

Remember:

  • Keep your data classes immutable to enhance code stability.
  • Leverage unions and sealed classes for complex data structures.
  • Consider custom serialization for specific scenarios.

With Flutter Freeze by your side, you’ll be well on your way to building exceptional and efficient Flutter applications!

Sources:

Follow Me:

https://www.linkedin.com/in/affan-minhas/

--

--