我有一个Enum SkillType:
enum SkillType {
Goal(0),
Assist(1),
Plusminus(2),
Hits(3),
Penalty(4),
ShotsOnGoal(5),
FaceoffsWon(6),
BlockedShots(7),
SavePercentage(8);
const SkillType(this.value);
final int value;
}我在一个与来自Server的响应相匹配的类中使用它:
import 'package:json_annotation/json_annotation.dart';
import '../enums/skillType.dart';
part 'teamskill.g.dart';
@JsonSerializable()
class TeamSkill {
final int TeamSkillId;
final int TeamId;
final SkillType Skilltype;
TeamSkill(this.TeamSkillId, this.TeamId, this.Skilltype);
factory TeamSkill.fromJson(Map<String, dynamic> json) =>
_$TeamSkillFromJson(json);
Map<String, dynamic> toJson() => _$TeamSkillToJson(this);
}当我从服务器获得响应时,SkillType枚举作为int被接收,我的代码提供了一个来自json_annotation的不受支持的值错误:
无效参数:
1不是受支持的值之一:目标、帮助、减法、命中、惩罚、ShotsOnGoal、FaceoffsWon、BlockedShots、(package:json_annotation/src/enum_helpers.dart:83:5) I/颤振(21747):#0 $enumDecode (package:head_coach_shl/dto/teamskill.g.dart:12:7) I/颤振(21747):#1 _$TeamSkillFromJson TeamSkill.fromJson I/颤振(21747):#2新TeamSkill.fromJson (package:head_coach_shl/dto/teamskill.dart:21:7) I/颤振(21747):#3 API.getSkillTree。(包装:head_coach_shl/api/api.dart:979:57)I/颤振(21747):#4
MappedListIterable.elementAt (飞镖:_ ListIterator.moveNext /可迭代.dart:413:31)i/颤振(21747):#5 ListIterator.moveNext(飞镖:_ListIterator.moveNext/iterable.dart:342:26)i/颤振(21747):#6新_GrowableList._ofEfficientLengthIterable (飞镖:核心补丁/可生长_array.dart:189:27)i/颤振(21747):#7
新_GrowableList.of (飞镖:核心补丁/可生长_array.dart:150:28)i/颤振(21747):#8新List.of (飞镖:核心补丁/阵列_patch.dart:51:28)i/颤振(21747):#9
ListIterable.toList (飞镖:21344)i/ API.getSkillTree (21747):#10 API.getSkillTree(包:head_coach_shl/api/api.dart:979:70)i/颤振(21747):i/颤振(21747):#11
_SkillTreeScreenState.loadDetails (package:head_coach_shl/screens/myTeam/skilltree.dart:57:22)
运行json_annotation版本4.6.0和json_serializable版本6.3.1 (和Dart 2.17.6)。
我一定是漏掉了一些关于强化后的鸟巢.
发布于 2022-08-17 13:33:00
用json注释解决了..。
import 'package:json_annotation/json_annotation.dart';
enum SkillType {
@JsonValue(0)
Goal(0),
@JsonValue(1)
Assist(1),
@JsonValue(2)
Plusminus(2),
@JsonValue(3)
Hits(3),
@JsonValue(4)
Penalty(4),
@JsonValue(5)
ShotsOnGoal(5),
@JsonValue(6)
FaceoffsWon(6),
@JsonValue(7)
BlockedShots(7),
@JsonValue(8)
SavePercentage(8);
const SkillType(this.value);
final int value;
}https://stackoverflow.com/questions/73389216
复制相似问题