Dart json可序列化-如何返回常量IconData

cfh9epnr  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(125)

将IconData存储在服务器上并使用JsonSerializable检索它,会导致Jenkin android构建版本出现以下错误。
此应用程序无法生成摇动图标字体树。它在以下位置具有IconData的非常数示例:
下面的代码用于构建图标数据对象:

@JsonSerializable(explicitToJson: true)
class DrawerIconDataDto extends IconData {
  const DrawerIconDataDto(
    super.codePoint, {
    super.fontFamily,
    super.fontPackage,
    super.matchTextDirection,
  });

  /// Create a new instance from a json.
  factory DrawerIconDataDto.fromJson(Map<String, dynamic> json) {
    return _$DrawerIconDataDtoFromJson(json);
  }

  /// Convert this instance to a json.
  @override
  Map<String, dynamic> toJson() => _$DrawerIconDataDtoToJson(this);
}

即使向下面的类添加const也会导致错误。我试图向工厂添加const,但这是不可能的。
如何使用JsonSerializable从服务器获取常量DataIcon对象,而不修改Jenkins配置(不允许)。

4ioopgfo

4ioopgfo1#

在Dart中,const意味着编译器可以在编译时确定值,显然,这不适用于从服务器检索的数据。
因此,您无法从服务器检索const数据。
您可以做的是在代码中将所有可能的IconData值作为const示例,并从服务器检索标识符。

相关问题