Flutter: Data Class, Union/Sealed Class, etc? … Go with Freezed 🥶
Freezed v1.1.0
I know… I know, we all hate that there’s not a simple way to create a Data Class in Dart. If you come from a beautiful world where you can use languages with features like in Kotlin you will feel frustrated trying to do this, but luckily we have a pretty cool package that makes the work for us, it is called: Freezed. It was created by Remi Rousselet (who also introduced Provider and Riverpod).
What dependencies you need to enable Freezed?
Let’s start…
Let’s think that we want to create a Data Class. Ok, if we go word by word we will end with something like this:
If we apply freezed, we will have a smaller and readable code instead
Notice these points:
- The class needs to use the generated mixin (because it adds superpowers). In this case _$User.
- The constructor needs to use = _User. You can also define different constructors.
- Because we’re generating code we need to specify user.freezed.dart (name of the file that will be generated) at the top of this file. This will generate a new file that contains generated code with all the cool stuff of Data Class.
- These classes are immutable!
The last thing to make the above works is that you need to generate the file mentioned above. In this case, it is user.freezed.dart, to do that you need to run the following command:
flutter pub run build_runner build --delete-conflicting-outputs
I know that at first sight, it looks more complicated, but believe me that when you start adding more fields, nested objects, and more classes it will save you to write a lot of boilerplate code.
Wants to enable JsonSerializer? sure thing…
To do this, you need to add the dependency mentioned at the top this article and then execute the build runner. Like this:
Union/Sealed Class ✨
This is the way to create a Union/Sealed class, if you haven’t worked with it before probably you’re asking yourself where you can apply it. Well, a pretty good example is BLoC, but I feel like it is better to explain it in a different post, so I explain how you can use it here:
Conclusion
A lot of people hate code generation for different reasons, like code clashing in the generated files, time that this task takes, etc. and it is more hatted when you need to run flutter builder runner manually a lot of times, and if you have multiple modules… man! you need to run it for all of them. You can also create some scripts or configure the build task to avoid writing it manually and do it smartly, but sometimes that just relieves some of the pain. The thing here is: Writing all this code on your own could be pretty painful and code generation saves you to write tons of code and that means it saves you time.
Freezed is a great package that is here to help you, if you don’t like things like code generation you can go without it using alternative ways like VS Code Data Class Extension (of course you will lose other features), use some other packages or write it by your own. Keep in mind freezed is not a required thing or indispensable package, but if you go without freezed when your project starts getting bigger and complex you may find yourself writing tons of code and boilerplate, and that could make your code unreadable or hard to read, exhaustive and complex. We all want things like Data Class built in, but the way of how flutter works (like tree shaking) is impossible for now. Freezed is something that does not fit for all teams so give it try, or try it in your next BLoC project and you will see a much more cleaner code, and always look for a happy coding path…