this post was submitted on 15 Jun 2023
6 points (100.0% liked)
Dart Programming Language
132 readers
1 users here now
This sublemmy is a place for everything related to the Dart programming language and its tools besides Flutter (use [email protected] for that)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Ooh is that a genuine option? Like if you were to model the same situation as in the Before/After example on their homepage, what would your code look like?
I have the same question. Using Dart native solutions and reducing code gen is definitely preferred but freezed isn't easy to replace
Replied above for a better comment hierarchy. Feel free to comment there if you have any questions/thoughts!
Since there are too many examples on the freezed README and the one at the top isn't a good use case to begin with (I like to keep my data models (DTOs) separate from entities, and DTOs are good enough with plain
json_serializable
), I'll provide an example from one of the projects I'm currently working on. It is still more verbose than it would usually be with freezed, however, I'm pretty fine with that. Also, it's worth noting that whenever I need acopyWith
, I still use codegen with copy_with_extension. It has a nicercopyWith
API and only handles that instead of a bunch of other stuff I don't necessarily need.And then using the sealed class itself becomes super-nice, like the following snippet (only wrapped in a function to state clearly where the variable is coming from):
It's also worth noting that freezed is switching to the Dart 3 pattern matching, too (see https://pub.dev/packages/freezed#legacy-union-types-and-sealed-classes).
Anyways, I think this is a matter of preference, and I've been historically avoiding codegen as much as possible. I'm glad it's now easier to do that, but freezed is still cool and could be really helpful for certain people/scenarios!
Thanks, so not quite as concise as using freezed, but using plain old Dart is a very nice benefit.