flutterdartflutter-freezedbuild-runner

Freezed: Could not resolve annotation for `int get hashCode`: @JSONKey(ignore: true)


When I try to generate freezed files on this classes, it gives me this error. I tried to rewrite hashCode getter on MappedLesson class but it didn't change anything.

line 1, column 4214 of package:derstakip_2/models/lat_models.freezed.dart: Could not resolve annotation for `int get hashCode`.
  ╷
1 │ @JsonKey(ignore: true)
  │ ^^^^^^^^^^^^^^^^^^^^^^
  ╵

These are the classes. I uesd freezed annotation only on the first class. Because other two classes (only first one included here, they've similar structure) are extended classes. Extended classes are not welcome in freezed unfortunately.

import 'package:derstakip_2/moor/tables.dart';
import 'package:drift/drift.dart' as drift;
import 'package:freezed_annotation/freezed_annotation.dart';

part 'lat_models.freezed.dart';
part 'lat_models.g.dart';

@freezed
class MappedLessonObject with _$MappedLessonObject {
  factory MappedLessonObject({
    required MappedLesson lesson,
    required List<MappedTopic> topics,
  }) = _MappedLessonObject;
  factory MappedLessonObject.fromJson(Map<String, dynamic> json) => _$MappedLessonObjectFromJson(json);
}

class MappedLesson extends MLesson {
  MappedLesson({
    required this.userIds,
    required super.id,
    required super.name,
  });
  final List<String> userIds;

  @override
  int get hashCode => id.hashCode ^ name.hashCode ^ userIds.hashCode;

  factory MappedLesson.fromJson(Map<String, dynamic> json, {drift.ValueSerializer? serializer}) {
    serializer ??= drift.driftRuntimeOptions.defaultSerializer;
    return MappedLesson(
      id: serializer.fromJson<String>(json['id']),
      name: serializer.fromJson<String>(json['name']),
      userIds: serializer.fromJson<List<String>>(json['userIds']),
    );
  }
  @override
  Map<String, dynamic> toJson({drift.ValueSerializer? serializer}) {
    serializer ??= drift.driftRuntimeOptions.defaultSerializer;
    return <String, dynamic>{
      'id': serializer.toJson<String>(id),
      'name': serializer.toJson<String>(name),
      'userIds': serializer.toJson<List<String>>(userIds),
    };
  }

  MappedLesson copyWith({String? id, String? name, List<String>? userIds}) => MappedLesson(
        id: id ?? this.id,
        name: name ?? this.name,
        userIds: userIds ?? this.userIds,
      );
}

Solution

  • This is because I was trying to use drift and json_annotation (for freezed dep.) imports at the same file. Generated file is still part of the file that I use two imports. drift and json_annotation has conflicts and the error occurs with this conflicts. When I renamed drift like import 'package:drift/drift.dart' as drift; and applied it to code, conflict resolved.

    class MappedLesson extends MLesson {
      MappedLesson({
        required this.userIds,
        required super.id,
        required super.name,
      });
      final List<String> userIds;
    
      @override
      int get hashCode => id.hashCode ^ name.hashCode ^ userIds.hashCode;
    
      factory MappedLesson.fromJson(Map<String, dynamic> json, {drift.ValueSerializer? serializer}) {
        serializer ??= drift.driftRuntimeOptions.defaultSerializer;
        return MappedLesson(
          id: serializer.fromJson<String>(json['id']),
          name: serializer.fromJson<String>(json['name']),
          userIds: serializer.fromJson<List<String>>(json['userIds']),
        );
      }
      @override
      Map<String, dynamic> toJson({drift.ValueSerializer? serializer}) {
        serializer ??= drift.driftRuntimeOptions.defaultSerializer;
        return <String, dynamic>{
          'id': serializer.toJson<String>(id),
          'name': serializer.toJson<String>(name),
          'userIds': serializer.toJson<List<String>>(userIds),
        };
      }
    
      MappedLesson copyWith({String? id, String? name, List<String>? userIds}) => MappedLesson(
            id: id ?? this.id,
            name: name ?? this.name,
            userIds: userIds ?? this.userIds,
          );
    }