Skip to content

if you use private unnamed constructors in inherited classes you can't use super.parameter #4346

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

Closed
escamoteur opened this issue May 1, 2025 · 1 comment
Labels
request Requests to resolve a particular developer problem

Comments

@escamoteur
Copy link

I have to classes in the same library (part-part-of)

class UserProxyTiny with ChangeNotifier, ImagePrecacheable {
  UserProxyTiny._({
    required this.isCurrentUser,
    UserTinyDto? userDataTiny,
    String? username,
  })  : _userDataTiny = userDataTiny,
        _username = username {

and

class UserProxyMini extends UserProxyTiny {
  UserProxyMini._({
    UserMiniDto? userMiniDto,
    required super.isCurrentUser,
  }) : _userDataMini = userMiniDto;

but the analyser complains that I can't call super because UserProxyTiny doesn't have an unnamed constructor. But how do you make private unnamed constructor?

interestingly

class UserProxyMini extends UserProxyTiny {
  UserProxyMini._({
    /// this has to be nullable, otherwise the derived classes could not
    /// call the super constructor with null
    UserMiniDto? userMiniDto,
    required super.isCurrentUser,
  })  : _userDataMini = userMiniDto,
        super._();

is accepted which is not really intuitive because I mix super.parameter with :super()

Is that intentional?

@escamoteur escamoteur added the request Requests to resolve a particular developer problem label May 1, 2025
@mraleph
Copy link
Member

mraleph commented May 1, 2025

Is that intentional?

Yes.

It has nothing to do with super.parameter. That's simply how constructors work: you need to specify which super constructor you want to call if it is not the default (unnamed) one. You will get the same error simply by trying to write:

class A {
  A._();
}

class B extends A {
  B._(); 
}

@mraleph mraleph closed this as completed May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants