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

toString override not working #1120

Open
johnnyggalt opened this issue Aug 23, 2024 · 3 comments
Open

toString override not working #1120

johnnyggalt opened this issue Aug 23, 2024 · 3 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@johnnyggalt
Copy link

Describe the bug
Overriding toString does not work. I feel like I must be doing something incredibly stupid, but have boiled this down to simplest thing that should work, and it doesn't.

To Reproduce

import 'package:freezed_annotation/freezed_annotation.dart';

part 'repro.freezed.dart';

@freezed
class Repro with _$Repro {
  const factory Repro({required int foo}) = _Repro;

  @override
  String toString() => 'My custom toString';
}

void doRepro() {
  const repro = Repro(foo: 42);

  // "Instance of '_$ReproImpl'"
  final reproToString = repro.toString();
}

Expected behavior
My toString implementation should be invoked, not that of the _$ReproImpl type. Even the IDE agrees it should be invoking my implementation because go-to-definition jumps there, but nope.

This is using latest:

dependencies:
  freezed_annotation: ^2.4.4

dev_dependencies:
  build_runner: ^2.4.12
  freezed: ^2.5.7
@get6
Copy link

get6 commented Oct 9, 2024

Do you try that this code?

@freezed
to
@Freezed(toStringOverride: false)

@johnnyggalt
Copy link
Author

@get6 Yes, tried that (I should have mentioned in my issue). It does not make any difference.

@johnnyggalt
Copy link
Author

I just circled back to this because I'm shipping to prod shortly, and I've identified a fix. This code does not work:

@freezed
class Repro with _$Repro {
  const factory Repro({required int foo}) = _Repro;

  @override
  String toString() => 'My custom toString';
}

This code does work:

@freezed
class Repro with _$Repro {
  Repro._();

  const factory Repro({required int foo}) = _Repro;

  @override
  String toString() => 'My custom toString';
}

So for some reason the private constructor is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

3 participants