getMapFromModelDataWithChild method

Map<String, dynamic> getMapFromModelDataWithChild({
  1. required dynamic model,
  2. required dynamic data,
  3. dynamic childDataList,
  4. dynamic childModelList,
})

Implementation

Map<String, dynamic> getMapFromModelDataWithChild({required dynamic model,required dynamic data, dynamic childDataList, dynamic childModelList}) {
  List<String>? fieldList = getModelFieldNameList(model);

  List<dynamic> childObjectListFromField = [];
  Map<String, String> fieldNameList = data.getDataTypeMap();

  fieldNameList.forEach((field, type) {
    if (type == "List<dynamic>") {
      childObjectListFromField.add(DatabaseHelper.getModel(field));
    }
  });
  Map<String, dynamic> dataMap = {};
  String fieldNamesTemp = data.toString();
  var namesStrings = fieldNamesTemp.split("{");

  String dataWithoutSecondBracketLeft = "";
  for (int i = 0; i < namesStrings.length; i++) {
    if (i == 1) {
      dataWithoutSecondBracketLeft = namesStrings[i];
    } else if (i == namesStrings.length - 1) {
      dataWithoutSecondBracketLeft =
          dataWithoutSecondBracketLeft + "{" + namesStrings[i];
    } else {
      dataWithoutSecondBracketLeft =
          dataWithoutSecondBracketLeft + "{" + namesStrings[i];
    }
  }

  String dataWithoutSecondBracketRight = dataWithoutSecondBracketLeft.substring(0,dataWithoutSecondBracketLeft.length-1);

  var fieldNameListTemp = dataWithoutSecondBracketRight.split(", ");

  for (int j = 0; j < fieldList!.length; j++) {
    bool isStarted = false;
    String key = fieldList[j];
    String? value = "";

    for (int i = 0; i < fieldNameListTemp.length; i++) {
      String singleDataset = fieldNameListTemp[i];

      if (singleDataset.startsWith(fieldList[j])) {
        value = getValueWithColonValidation(fieldNameListTemp[i]);
        dataMap[key] = value;
        if (value == "null") {
          value = null;
        }
        isStarted = true;
      } else {
        if (isStarted) {
          if (j < fieldList.length - 1) {
            if (singleDataset.startsWith(fieldList[j + 1])) {
              isStarted = false;
              break;
            } else {
              value = value! + ", " + fieldNameListTemp[i];
              dataMap[key] = value;
              if (value == "null") {
                value = null;
              }
              isStarted = true;
            }
          } else {
            value = value! + ", " + fieldNameListTemp[i];
            if (value == "null") {
              value = null;
            }
            dataMap[key] = value;
            isStarted = true;
          }
        }
      }
    }
  }

  if(childDataList != null){

    if(childModelList!=null){

      for (int i = 0; i < childObjectListFromField.length; i++) {
        for (int j = 0; j < childModelList.length; j++) {
          if(childObjectListFromField[i].runtimeType.toString() == childModelList[j].runtimeType.toString()){
            List<dynamic> datas = [];
            for (int k = 0; k < childDataList[j].length; k++) {
              datas.add(childDataList[j][k]);
            }
            dataMap[childObjectListFromField[i].runtimeType.toString()] = datas;
            break;
          }
        }
      }
    }else{
      if(childObjectListFromField.length == childDataList.length) {
        for (int i = 0; i < childDataList.length; i++) {
          List<dynamic> datas = [];
          for (int j = 0; j < childDataList[i].length; j++) {
            datas.add(childDataList[i][j]);
          }
          dataMap[childObjectListFromField[i].runtimeType.toString()] = datas;
        }
      }else{
        debugPrint("Child data list not matched");
      }
    }
  }

  dataMap.forEach((key, value) {
    if (value == "null") {
      dataMap[key] = null;
    }
  });

  debugPrint("Data map : " + dataMap.toString());

  return dataMap;
}