无法使用冻结包为泛型生成 fromJson() 和 toJson()

Unable to generate fromJson() and toJson() for generics using freezed package

我们正在尝试创建通用类别 class。目前,我们不确定类别是整数还是 UUID 作为键。因此,我们现在需要 id 是通用的。一切正常。但是,我们无法使用 freezed 包生成 fromJson() 和 toJson()。

import 'package:freezed_annotation/freezed_annotation.dart';

part 'category.freezed.dart';
part 'category.g.dart';

@freezed
@JsonSerializable(genericArgumentFactories: true)
class Category<T> with _$Category<T> {
  factory Category({
    required T id,
    required String name,
    required String imageUrl,
  }) = _Category;

  factory Category.fromJson(Map<String, dynamic> json) =>
      _$CategoryFromJson(json);
}

错误:

Could not generate `fromJson` code for `id` because of type `T` (type parameter).
To support type parameters (generic types) you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
* Set `JsonSerializable.genericArgumentFactories` to `true`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/genericArgumentFactories.html
package:mobile/data/models/category.freezed.dart:144:11
    ╷
144 │   final T id;
    │           ^^
    ╵
[SEVERE] Failed after 2.4s
pub finished with exit code 1

如错误消息所示,我使用了 @JsonSerializable(genericArgumentFactories: true) 注释,但它没有按建议工作。我怎样才能得到 fromJson()toJson() 方法冻结泛型?

目前不支持的功能。

来源:Issue #616