getByName<T> method

T? getByName<T>(
  1. String name, {
  2. bool isFormat = false,
})

Get the value of the specified field name.

If the type of the requested return value is different from the type of this field defined, then an output conversion is performed. And if you specify a field name that is not defined, it throws an exception.

Implementation

T? getByName<T>(String name,{bool isFormat=false}){
  FieldSchema? fs = _schema?.fieldMap[name];
  if(fs == null){
    throw Exception("The specified field does not exist. name=$name, schema=$_schema");
  }
  Object? value = fs.isView ? fs.viewValue(_dataSet, this) : _values![name];
  T? ret;
  if(value == null){
    if(fs.type != T && isFormat){
      ret = fs.formatValue(_dataSet, this, value);
    }else{
      ret = null;
    }
  }else{
    if(!isFormat && value is T){
      ret = value as T;
    }else{
      value = fs.formatValue(_dataSet, this, value);
      if(value is T){
        ret = value;
      }else{
        if(value is String){
          if(T == int){
            ret = int.parse(value) as T;
          }else if(T == double){
            ret = double.parse(value) as T;
          }else if(T == bool){
            value = value.toLowerCase().trim();
            ret = (value == "true" || value == "on" || value == "yes") as T;
          }else{
            ret = value as T;
          }
        }else if(value is bool){
          if(T == int){
            ret = (value == true ? 1 : 0) as T;
          }else if(T == double){
            ret = (value == true ? 1.0 : 0.0) as T;
          }else if(T == String){
            ret = (value == true ? "true" : "false") as T;
          }else{
            ret = value as T;
          }
        }else if(value is num){
          if(T == int){
            ret = (value.toInt()) as T;
          }else if(T == double){
            ret = (value.toDouble()) as T;
          }else if(T == String){
            ret = (value.toString()) as T;
          }else{
            ret = value as T;
          }
        }
      }
    }
  }
  return ret;
}