如何使Freezed对象采用泛型类型?我想做这个:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/src/entity_types/option_entity.dart';
part 'vegan_item_tag.freezed.dart';
part 'vegan_item_tag.g.dart';
@freezed
abstract class VeganItemTag<T>
with _$VeganItemTag<T>
implements OptionEntity<T> {
const factory VeganItemTag({int? iconCodePoint, T? id, String? name}) =
_VeganItemTag;
const VeganItemTag._();
factory VeganItemTag.fromJson(Map<String, dynamic> json) =>
_$VeganItemTagFromJson(json);
}
我已经尝试@With.fromString('AdministrativeArea<House>')
从文档中使用,但无法将其正确应用于此类。
错误之一:
lib/src/common/enums/tags/common/vegan_item_tag.freezed.dart:142:32:错误:位置参数太少:需要 2 个,给定 1 个。
$ $_VeganItemTagFromJson(json);
认为我可能在正确的轨道上,但它不再生成vegan_item_tag.g.dart
文件:
@freezed
abstract class VeganItemTag<T>
with _$VeganItemTag<T>
implements OptionEntity<T> {
const factory VeganItemTag(
{required int iconCodePoint,
required T id,
required String name}) = _VeganItemTag;
const VeganItemTag._();
factory VeganItemTag.fromJson(
Map<String, Object?> json,
T Function(Object?) fromJsonT,
) => VeganItemTag(
iconCodePoint: json['iconCodePoint'] as int,
id: fromJsonT(json['id']),
name: json['name'] as String,
);
}