Flutter: Data Class, Union/Sealed Class, etc? … Go with Freezed 🥶

Let’s start…

  • 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!
flutter pub run build_runner build --delete-conflicting-outputs

Wants to enable JsonSerializer? sure thing…

Union/Sealed Class ✨




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pablo Reyes

Pablo Reyes

Senior Android & Flutter Developer