toEntity method

T toEntity(
  1. Map<String, dynamic> map
)

Implementation

T toEntity(Map<String, dynamic> map){
  T data = newInstance();
  for(NUIEntField field in fields()){
//      if(field.type == NUIEntType.OBJECT && field.entity != null){
//        field.runSetter(data, toObject(field.entity, map[field.name]));
//      }
//      else if(field.type == NUIEntType.LIST_OBJECT && field.entity != null){
//        field.runSetter(data, toListOfObjects(field.getTargetEntity(), map[field.name]));
//      }
//      else if(field.type == NUIEntType.LIST_INT){
//        field.runSetter(data, toList<int>(map[field.name]));
//      }
//      else if(field.type == NUIEntType.LIST_STRING){
//        field.runSetter(data, toList<String>(map[field.name]));
//      }
//      else if(field.type == NUIEntType.LIST_DOUBLE){
//        field.runSetter(data, asListOfObjects<double>(field.entity, map[field.name]));
//      }
    try{
      if(field.type == NUIEntType.DATE){
        field.runSetter(data, asDateTime(map[field.name], format: field.additionalFormat));
      }
      else if(field.type == NUIEntType.MAP){
        field.runSetter(data, asMap(map[field.name]));
      }
      else if(field.type == NUIEntType.INTEGER && map[field.name] != null && map[field.name] is int){
        field.runSetter(data, (map[field.name] as int).toInt());
      }
      else if(field.type == NUIEntType.DOUBLE && map[field.name] != null && map[field.name] is double){
        field.runSetter(data, (map[field.name] as double).toDouble());
      }
      else if(field.type == NUIEntType.BOOLEAN){
        final value = map[field.name];
        if(value is String){
          final isTrue = match(value, "true");
          field.runSetter(data, isTrue);
        }
        else if(value is int){
          if(value == 0){
            field.runSetter(data, false);
          }
          else{
            field.runSetter(data, true);
          }
        }
        else{
          field.runSetter(data, value);
        }
      }
      else {
        field.runSetter(data, map[field.name]);
      }
    }catch(e){
      log("NUIJson -> Failed to read field : ${field.name} of type : ${field.type} with exception : ${e.toString()}");
    }

  }
  return data;
}