convertValue<T> method

  1. @override
T? convertValue<T>(
  1. JsEvalResult jsValue
)
override

Implementation

@override
T? convertValue<T>(JsEvalResult jsValue) {
  if (jSValueIsNull(_globalContext, jsValue.rawResult) == 1) {
    return null;
  } else if (jSValueIsString(_globalContext, jsValue.rawResult) == 1) {
    return _getJsValue(jsValue.rawResult) as T;
  } else if (jSValueIsBoolean(_globalContext, jsValue.rawResult) == 1) {
    return (_getJsValue(jsValue.rawResult) == "true") as T;
  } else if (jSValueIsNumber(_globalContext, jsValue.rawResult) == 1) {
    String valueString = _getJsValue(jsValue.rawResult);

    if (valueString.contains(".")) {
      try {
        return double.parse(valueString) as T;
      } on TypeError {
        print('Failed to cast $valueString... returning null');
        return null;
      }
    } else {
      try {
        return int.parse(valueString) as T;
      } on TypeError {
        print('Failed to cast $valueString... returning null');
        return null;
      }
    }
  } else if (jSValueIsObject(_globalContext, jsValue.rawResult) == 1 ||
      jSValueIsArray(_globalContext, jsValue.rawResult) == 1) {
    JSValue objValue = JSValuePointer(jsValue.rawResult).getValue(context);
    String serialized = objValue.createJSONString().string!;
    return jsonDecode(serialized);
  } else {
    return null;
  }
}