【问题标题】:Dart: Get enum from String?Dart:从字符串中获取枚举?
【发布时间】:2017-08-01 03:29:09
【问题描述】:

您好,我在 Google Maps dart 库中有以下类,并希望将 Google Style JSON 解析为 Dart 面向对象格式。

在库中(不能更改!)我有以下枚举:

part of google_maps.src;

@jsEnum
class MapTypeStyleElementType extends JsEnum {
  static final values = <MapTypeStyleElementType>[
    ALL,
    GEOMETRY,
    GEOMETRY_FILL,
    GEOMETRY_STROKE,
    LABELS,
    LABELS_ICON,
    LABELS_TEXT,
    LABELS_TEXT_FILL,
    LABELS_TEXT_STROKE
  ];
  static final ALL = new MapTypeStyleElementType._('all');
  static final GEOMETRY = new MapTypeStyleElementType._('geometry');
  static final GEOMETRY_FILL = new MapTypeStyleElementType._('geometry.fill');
  static final GEOMETRY_STROKE =
      new MapTypeStyleElementType._('geometry.stroke');
  static final LABELS = new MapTypeStyleElementType._('labels');
  static final LABELS_ICON = new MapTypeStyleElementType._('labels.icon');
  static final LABELS_TEXT = new MapTypeStyleElementType._('labels.text');
  static final LABELS_TEXT_FILL =
      new MapTypeStyleElementType._('labels.text.fill');
  static final LABELS_TEXT_STROKE =
      new MapTypeStyleElementType._('labels.text.stroke');
  MapTypeStyleElementType._(o) : super.created(o);
}

这是 JSON 的示例 JSON:

  {
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#f5f5f5"
      }
    ]
  }

这是我尝试过的(我的解析器的一部分):

m.elementType = MapTypeStyleElementType.values[value];

value 是字符串“geometry”,我想要返回 MapTypeStyleElementType.GEOMETRY

这是错误堆栈跟踪:

EXCEPTION: Invalid argument(s): geometry(anonymous function) @ VM1449:1 VM1449:1 STACKTRACE:(anonymous function) @ VM1449:1 VM1449:1
#0      List.[] (dart:core-patch/growable_array.dart:153)
#1      Parser.getMapStyle.<anonymous closure> (package:xxx/map_component/mapstyle.dart:17:59)
#2      _HashVMBase&MapMixin&&_LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:348)
#3      Parser.getMapStyle (package:xxx/map_component/mapstyle.dart:15:13)

我尝试的是简单地从给定的字符串创建足够的枚举而不为每个值创建 if/else 语句(因为库中有数百个枚举重视这真的很重要!)。不幸的是构造函数是私有的我怎么能做到这一点?

【问题讨论】:

    标签: json parsing enums dart


    【解决方案1】:

    JsEnum 扩展 JsRef 将字符串存储为私有变量:_value

    但是_value是通过函数暴露的:asJs:

    /// Returns the underlying js value corresponding to [o] if [o] is a [JsRef]
    /// (usually [JsEnumBase] or [JsInterface]). Otherwise it returns [o].
    asJs(o) => o is JsRef ? o._value : o;
    

    【讨论】:

      【解决方案2】:

      如果您可以访问传递给构造函数的字符串,则可以通过遍历值列表来创建所需的映射。

      var elementTypes = <String, MapTypeStyleElementType>{};
      for (var value in MapTypeStyleElementType.values) {
        // just guessing on the JsEnum API here...
        elementTypes[value.name] = value;
      }
      

      然后在你的解析代码中你可以写

      m.elementType = elementTypes[value];
      

      【讨论】:

      • 好主意,但不幸的是它没有帮助,因为 value 只有与 Object 相同的属性/方法:value.hashCode()、value.runtimeType()、value.toString() 和 value.noSuchMethod(调用调用)。您认为我可以使用哈希码进行比较吗?
      • 顺便说一句。 JsEnum 来自import 'package:js_wrapping/js_wrapping.dart';
      • 看起来你可以使用asJs function提取值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2020-07-11
      • 2013-07-18
      • 2021-12-04
      • 2021-04-04
      相关资源
      最近更新 更多