cast<T> function

T? cast<T>(
  1. dynamic x
)

doubleString 타입의 경우는 특수 처리를 하고 있다. xnum 타입이고 Tdouble 타입일 경우, x의 값이 double로 반환된다. TString 타입일 경우, x.toString의 결과가 반환된다.

Implementation

T? cast<T>(final dynamic x) {
  if (x == null) {
    return null;
  }
  try {
    switch (T) {
      case const (double):
        return (x as num).toDouble() as T;
      case const (String):
        return x == null ? x : x.toString();
      case const (int):
        return (x as num).toInt() as T;
      default:
        return x as T;
    }
  } catch (_, __) {
    return null;
  }
}