freezed
freezed copied to clipboard
Feature Request: Option to Generate when Method for Sealed Classes in v3+
Is your feature request related to a problem? Please describe. In version 3.+, the when method has been removed, which affects readability and convenience, especially when used in Flutter widgets.
Describe the solution you'd like Provide an option to generate the when method alongside the new sealed class features. This would allow developers to maintain cleaner and more expressive widget code, particularly when dealing with multiple subclasses.
Describe alternatives you've considered While pattern matching with switch expressions or map methods works, they tend to be more verbose and less readable in widget trees. The when method offered a more elegant and concise alternative.
Additional context Loving the new sealed class solution! It brings powerful expressiveness to Dart. However, the when method was still very handy and readable — especially in UI code where clarity matters. An optional generation of when would provide the best of both worlds.
Please restore 'when' and 'maybe when' in the case of blocks, because in order to transition to v3, it is necessary to migrate hundreds or even thousands of blocks.
This seems to bother a few people. So I don't mind adding it back, to help folks migrate over a longer period.
Still, I'm a bit busy right now. If someone fancy adding it back, I'll be happy to review a PR :)
This will have to be added back as an extension method, for the sake of inheritance.
My biggest mistake in 2025 was upgrading Freezed to version 3 — the whole codebase is broken. I hope helper functions will be added soon. Many people use BLoC with Freezed, where pattern-matching functions like map and when are crucial.
I'm not moving to v3 until this becomes available. I have thousands of when calls in all my projects and it's clean and works very well.
Do we have an update on this topic?
Removing the .map/.when extensions from the Freezed-generated files requires quite a bit of refactoring when migrating to v3.0, especially when the entire project relies on them in both the UI and Bloc implementations.
On it.
@rrousselGit Thank you for the prompt update.
Keep up the good work 👍