a14n / dart-google-maps

A library to use Google Maps JavaScript API v3 from Dart scripts.
Apache License 2.0
130 stars 66 forks source link

Possibility for Enum to create from / compare with string value? #44

Closed Blackbam1337 closed 7 years ago

Blackbam1337 commented 7 years ago

Example:

@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);
}

I was trying to create a parser for JSON created with https://mapstyle.withgoogle.com/. As the constructor of MapTypeStyleElementType is private there is no possibility (which I found) to find the correct Enum value for a correlating String value. If I have 'geometry' as a String - how do I get the Enum for it?

a14n commented 7 years ago

You can use asJs to get the underlying js value :

import 'package:js_wrapping/js_wrapping.dart' show asJs;
main() {
  MapTypeStyleElementType.values.firstWhere((e) => asJs(e) == typeAsString);
}