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 · 6 comments
Open

toString override not working #1120

johnnyggalt opened this issue Aug 23, 2024 · 6 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.

@mansourzaki
Copy link

@johnnyggalt
check this #170 (comment)

@ChunhThanhDe
Copy link

Hi, @johnnyggalt
You can see the author's explanation here

https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models

@johnnyggalt
Copy link
Author

Hi, @johnnyggalt You can see the author's explanation here

https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models

Yeah, but the explanation doesn't really fit my experience. The problem is that in my example, there is no compile error reported, nor is there a runtime error. Instead, the runtime behavior is not what you'd expect (the override is not called). If there was at least an error message then it wouldn't have caused me so much confusion.

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

5 participants