Freezed: Unable to add getters (`_()` defined and `with` replaced with `implements`)

Created on 4 Apr 2020  Â·  4Comments  Â·  Source: rrousselGit/freezed

I'm getting weird error on bare simple class:

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'todo.freezed.dart';
part 'todo.g.dart';

@freezed
abstract class Todo implements _$Todo {
  const Todo._();

  const factory Todo({
    @nullable String id,
    @required String text,
  }) = _Todo;

  factory Todo.fromJson(Map<String, dynamic> json) => _$TodoFromJson(json);

  bool get isValid => text != '';
}

Output is:

[SEVERE] freezed:freezed on lib/models/todo.dart:
Error running FreezedGenerator
@freezed cannot be used on classes with unimplemented getters
package:fideli_app/models/todo.dart:8:16
  â•·
8 │ abstract class Todo implements _$Todo {
  │                ^^^^
  ╵

I tried to fix it myself in freezed source by replacing this line with:

-      if (field.getter != null && !field.getter.isSynthetic && field.hasLate) {
+      if (field.getter != null && !field.getter.isSynthetic) {
+        if (!field.hasLate) continue;

and it fixed the issue in my project (using the path dependency option) but I was getting too many errors in freezed tests then, so I didn't make any PR....

I'm not expert in dart, but for me it sounds like the bug is at that line and might have been introduced when the late feature came in

bug

Most helpful comment

Thanks for the report, I will take a look

You can use extension methods in the mean time

All 4 comments

Actually I just tried even without the json part of it and I got the same issue:

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'todo.freezed.dart';

@freezed
abstract class Todo implements _$Todo {
  const Todo._();

  const factory Todo({
    @nullable String id,
    @required String text,
  }) = _Todo;

  bool get isValid {
    return text != '';
  }
}

Output:

[SEVERE] freezed:freezed on lib/models/todo.dart:
Error running FreezedGenerator
@freezed cannot be used on classes with unimplemented getters
package:fideli_app/models/todo.dart:8:16
  â•·
8 │ abstract class Todo implements _$Todo {
  │                ^^^^
  ╵

FYI:

dart --version
Dart VM version: 2.7.2 (Mon Mar 23 22:11:27 2020 +0100) on "macos_x64"

build_runner: 1.8.1
freezed: 0.10.4
freezed_annotation: 0.7.1
json_serializable: 3.3.0

Thanks for the report, I will take a look

You can use extension methods in the mean time

I'm hitting the same problem. Any update on this? I'm using freezed: ^0.12.5

Upgraded to 0.12.6 and the problem is disappeared!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vedantrathore picture vedantrathore  Â·  6Comments

jonasbark picture jonasbark  Â·  3Comments

pblinux picture pblinux  Â·  4Comments

rakakhrl picture rakakhrl  Â·  6Comments

tbm98 picture tbm98  Â·  3Comments