convertModelToJson method

Map<String, dynamic> convertModelToJson(
  1. Map<String, ModelEntity> models
)

Implementation

Map<String, dynamic> convertModelToJson(Map<String, ModelEntity> models) {
  final res = <String, dynamic>{};
  for (final field in fields) {
    if (field.variable is StringVar) {
      res[field.jsonKey] = (field.variable as StringVar).data;
      continue;
    }
    if (field.variable is StringListVar) {
      res[field.jsonKey] = (field.variable as StringListVar).data;
      continue;
    }
    if (field.variable is IntVar) {
      res[field.jsonKey] = (field.variable as IntVar).data;
      continue;
    }
    if (field.variable is IntListVar) {
      res[field.jsonKey] = (field.variable as IntListVar).data;
      continue;
    }
    if (field.variable is DoubleVar) {
      res[field.jsonKey] = (field.variable as DoubleVar).data;
      continue;
    }
    if (field.variable is DoubleListVar) {
      res[field.jsonKey] = (field.variable as DoubleListVar).data;
      continue;
    }
    if (field.variable is BoolVar) {
      res[field.jsonKey] = (field.variable as BoolVar).data;
      continue;
    }
    if (field.variable is BoolListVar) {
      res[field.jsonKey] = (field.variable as BoolListVar).data;
      continue;
    }
    if (field.variable is JsonVar) {
      res[field.jsonKey] = (field.variable as JsonVar).data;
      continue;
    }
    if (field.variable is EnumVar) {
      res[field.jsonKey] = (field.variable as EnumVar).value;
      continue;
    }
    if (field.variable is DateTimeVar) {
      res[field.jsonKey] =
          (field.variable as DateTimeVar).data?.toIso8601String();
      continue;
    }
    if (field.variable is DateTimeListVar) {
      res[field.jsonKey] = (field.variable as DateTimeListVar)
          .data
          ?.map((e) => e?.toIso8601String())
          .toList();
      continue;
    }
    if (field.variable is ModelVar) {
      final model = models[(field.variable as ModelVar).modelID];
      res[field.jsonKey] =
          (field.variable as ModelVar?)?.model?.convertModelToJson(models) ??
              model?.convertModelToJson(models);
      continue;
    }
    if (field.variable is ModelListVar) {
      res[field.jsonKey] = (field.variable as ModelListVar?)
          ?.models
          ?.map((e) => e.convertModelToJson(models));
      continue;
    }
  }
  return res;
}