Skip to content

Feature Request: Option to Generate when Method for Sealed Classes in v3+ #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Peng-Qian opened this issue Mar 21, 2025 · 3 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@Peng-Qian
Copy link

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.

@PrzemyslawPluszowy
Copy link

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.

@rrousselGit
Copy link
Owner

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.

@mqhamdam
Copy link

mqhamdam commented Apr 4, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants