Skip to content
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

feat(vscode): add useUnionTypes setting #4348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zbarbuto
Copy link

@zbarbuto zbarbuto commented Jan 30, 2025

Status

READY

Breaking Changes

NO

Description

Adds a new useUnionTypes setting to both newBlocTemplate and newCubitTemplate. Defaults to true to backwards compatibility. When turned off, state classes will be generated as a single freezed class rather than using unions.

Also brings support for the existing useSealedClasses to freezed state generation. I'm happy to remove this from the change set if you'd prefer not changing two things in the same PR but it did seem harmless enough...

Example class generated with the setting as true (the default)

part of 'foo_cubit.dart';

@freezed
sealed class FooState with _$FooState {
  const factory FooState.initial() = _Initial;
}

Example class generated with the setting as false

part of 'foo_bloc.dart';

@freezed
sealed class FooState with _$FooState {
  const factory FooState() = _FooState;
}

This is useful if you prefer states that can rely solely on the copyWith generated from freezed to emit state changes. For example:

@freezed
sealed class CounterState with _$CounterState {
  const factory CounterState({ required int count }) = _CounterState;
}

No need for an initial state here and for changes we can just use emit(state.copyWith( count: state.count + 1 )).

Or if you prefer to maintain existing data between states:

@freezed
sealed class TodoState with _$TodoState {
  const factory TodoState({ required bool isLoading, List<Todo>? data }) = _TodoState;
}

Here, we can trigger loading without emitting a TodoState.loading() which would lost our current data with emit(state.copyWith( loading: true )).

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Adds a new `useUnionTypes` setting to both `newBlocTemplate` and
`newCubitTemplate`. Defaults to `true` to backwards compatibility.
When turned off, state classes will be generated as a single freezed
class rather than using unions.

Also brings support for the existing `useSealedClasses` to freezed
state generation.
@zbarbuto zbarbuto requested a review from felangel as a code owner January 30, 2025 01:15
@felangel
Copy link
Owner

Thanks so much for the contribution! I think we'll likely want this setting to apply to the other templates as well (not just freezed).

I'll take a closer look shortly, apologies for the delay!

@felangel felangel added extension This issue relates to bloc extensions enhancement New feature or request labels Feb 17, 2025
@zbarbuto
Copy link
Author

zbarbuto commented Feb 17, 2025

Thanks for taking a look @felangel

So basically all the templates like this:

@immutable
${classPrefix} class ${pascalCaseBlocName}State {}

${subclassPrefix}class ${pascalCaseBlocName}Initial extends ${pascalCaseBlocName}State {}

should omit the Initial version class if useSealedClasses is off and just be the first 2 lines?

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

Successfully merging this pull request may close these issues.

2 participants