Skip to content

fix: Freezed requires abstract class or multiple factory constructors for non-abstract class with single factory #4815

@Daeon97

Description

@Daeon97

Describe the bug
I am using Freezed with BLoC. I have an event class auth_event.dart, viz:

part of 'auth_bloc.dart';

@freezed
... AuthEvent with _$AuthEvent {
  ...
}

When defining a non-abstract @freezed class with a single factory constructor that has named parameters, Freezed generates an error: Missing concrete implementation of 'getter _$AuthEvent.authMethod'. However, this error does not occur when:

  • The class is declared as abstract, or
  • The class has two or more factory constructors

This behaviour seems inconsistent and suggests Freezed is treating single-constructor non-abstract classes differently than multi-constructor classes or abstract classes.

To Reproduce

enum AuthMethod { apple, google }

Case 1 (Error):

@freezed
class AuthEvent with _$AuthEvent {
  const factory AuthEvent.authenticate({required AuthMethod authMethod}) = _Authenticate;
}

Error: Missing concrete implementation of 'getter _$AuthEvent.authMethod'

Case 2 (Works):

@freezed
abstract class AuthEvent with _$AuthEvent {
  const factory AuthEvent.authenticate({required AuthMethod authMethod}) = _Authenticate;
}

Case 3 (Works):

@freezed
class AuthEvent with _$AuthEvent {
  const factory AuthEvent.authenticate({required AuthMethod authMethod}) = _Authenticate;
  const factory AuthEvent.authenticateWithApple() = _AuthenticateWithApple;
}

Case 4 (Works):

@freezed
abstract class AuthEvent with _$AuthEvent {
  const factory AuthEvent.authenticate({required AuthMethod authMethod}) = _Authenticate;
  const factory AuthEvent.authenticateWithApple() = _AuthenticateWithApple;
}

Expected behavior
All three cases should either consistently require abstract or should work identically regardless of whether the class is abstract or has single/multiple factory constructors. The code generation should not have different behavior based on the number of factory constructors defined

Additional Context
I have create same issue on the Freezed repo here rrousselGit/freezed#1351 however I decided to create this issue here too because I am using bloc plugin with Android studio to create boilerplate. Looking at the Freezed docs, most of the examples there mark the class as abstract. If the requirement is to make the event classes abstract then probably the plugin needs to be updated to handle this scenario. However this behaviour is quiet confusing to me and I require some explanation to understand what may be going on here. So pending a response on what may be going on here, it is possible that a small change may or may not be needed for the plugin

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions