decodeJson method

Future<Map<String, List<Map<String, dynamic>>>> decodeJson({
  1. required Map<String, dynamic> jsonResponse,
})

Implementation

Future<Map<String, List<Map<String, dynamic>>>> decodeJson(
    {required Map<String, dynamic> jsonResponse}) async {
  Map<String, List<Map<String, dynamic>>> derived =
      <String, List<Map<String, dynamic>>>{};

  // Sort FormDetails by Sequence
  if (jsonResponse.containsKey(ConstantName.formDetails)) {
    List<Map<String, dynamic>> formDetails = List<Map<String, dynamic>>.from(
        jsonResponse[ConstantName.formDetails]);

    if (formDetails.isNotEmpty) {
      formDetails.sort((a, b) =>
          a[ConstantName.sequence].compareTo(b[ConstantName.sequence]));
      // Printing the sorted FormDetails
      // print('formDetails: $formDetails');

      // --> add {controller : value, fieldName: value} to formDetails and assign value to updateRetailerDetailsMap[fieldName]
      for (var detail in formDetails) {
        // detail[ConstantName.clientRetailerId] = retailerID.text;

        // --> temporary function:
        if (detail.containsKey(ConstantName.groupBy)) {
          detail[ConstantName.groupBy] =
              detail[ConstantName.groupBy] ?? ApiConstant.groupbynull;
        }
        // --> add "Select" into the options String start
        if (detail.containsKey(ConstantName.optionValues)) {
          var currentOptions = detail[ConstantName.optionValues];
          if (currentOptions == null ||
              currentOptions.toString().trim() == '') {
            detail[ConstantName.optionValues] = ConstantName.dropDownSelect;
          } else {
            detail[ConstantName.optionValues] =
                '${ConstantName.dropDownSelect},$currentOptions';
          }
        }
        // --> add "Select" into the options String end

        // --> add value and Set Key:value start
        String fieldNameKey = ErrorHandling.getContainsKey(
            detail: detail, inputKey: ConstantName.fieldName);

        String? fieldMatchValue = detail[fieldNameKey];
        if (fieldMatchValue != null) {
          var controllerValue;
          try {
            String retailerMatchKey = ErrorHandling.getContainsKey(
                detail: retailerDetailsMap, inputKey: fieldMatchValue);
            controllerValue = retailerDetailsMap[retailerMatchKey];
          } catch (e) {}
          detail[ConstantName.controller] = controllerValue ?? '';
          // (1st Process) Assigning key and value
          // uploadRetailerDetailsMap[fieldMatchValue] = controllerValue;
          // Need to check:
          uploadRetailerDetailsMap[fieldMatchValue] =
              controllerValue?.toString() ?? '';
        }
        // --> add value and Set Key:value end

        // --> Set remarks start
        String rejectMatchKey = ErrorHandling.getContainsKey(
            detail: detail, inputKey: ConstantName.rejectRemaks);

        String? rejectMatchValue = detail[rejectMatchKey];
        if (rejectMatchValue != null) {
          var remark;
          try {
            String retailerMatchKey = ErrorHandling.getContainsKey(
                detail: retailerDetailsMap, inputKey: rejectMatchValue);
            remark = retailerDetailsMap[retailerMatchKey];
          } catch (e) {}
          detail[rejectMatchKey] = remark ?? '';
        }
        // --> Set remarks end
      }

      await setDataToLocalDB(
          tableName: ConstantName.formDetailsTable,
          dataList: List<Map<String, dynamic>>.from(formDetails));

      // --> Next process
      for (int i = 0; i < formDetails.length; i++) {
        // --> Add keys: formDependable & formApi
        formDetails[i].addAll(<String, List<Map<String, dynamic>>>{
          ConstantName.formDependable: <Map<String, dynamic>>[],
          ConstantName.formApi: <Map<String, dynamic>>[]
        });

        if (formDetails[i][ConstantName.groupBy] != null) {
          derived
              .putIfAbsent(formDetails[i][ConstantName.groupBy],
                  () => <Map<String, dynamic>>[])
              .add(formDetails[i]);
        }
      }
      // print('derived:<1> $derived');
    }
  }

  // --> Next process
  if (jsonResponse.containsKey(ConstantName.formDependable)) {
    await setDataToLocalDB(
        tableName: ConstantName.formDependable,
        dataList: List<Map<String, dynamic>>.from(
            jsonResponse[ConstantName.formDependable]));

    addDataToDerived(
        data: List<Map<String, dynamic>>.from(
            jsonResponse[ConstantName.formDependable]),
        matchKey: ConstantName.columnID,
        nestedKey: ConstantName.formDependable,
        derived: derived);
  }

  // --> Next process
  if (jsonResponse.containsKey(ConstantName.formApi)) {
    await setDataToLocalDB(
        tableName: ConstantName.formApiTable,
        dataList: List<Map<String, dynamic>>.from(
            jsonResponse[ConstantName.formApi]));

    addDataToDerived(
        data: List<Map<String, dynamic>>.from(
            jsonResponse[ConstantName.formApi]),
        matchKey: ConstantName.columnID,
        nestedKey: ConstantName.formApi,
        derived: derived);
  }

  // --> Next process
  if (jsonResponse.containsKey(ConstantName.formJson)) {
    List<Map<String, dynamic>> formJson =
        List<Map<String, dynamic>>.from(jsonResponse[ConstantName.formJson]);

    for (var map in formJson) {
      String? inputKey = map[ConstantName.fieldName];
      if (inputKey != null && inputKey.trim().isNotEmpty) {
        String foundKey = ErrorHandling.getContainsKey(
            detail: retailerDetailsMap, inputKey: inputKey);

        // (2st Process) Assigning key and value
        uploadRetailerDetailsMap[inputKey] = retailerDetailsMap[foundKey];
      }
    }

    // --> (3rd Process) Assigning value by key start:
    String roleKey = ErrorHandling.getContainsKey(
        detail: uploadRetailerDetailsMap, inputKey: ConstantName.role);

    if (roleKey.trim().isNotEmpty) {
      String role = await SharedPreferenceHelper.getStringValueFromKey(
          SharedPreferenceKey.role);
      uploadRetailerDetailsMap[roleKey] = role;
    }

    String userMatchKey = ErrorHandling.getContainsKey(
        detail: uploadRetailerDetailsMap, inputKey: ConstantName.userName);

    if (userMatchKey.trim().isNotEmpty) {
      String? userSp = await SharedPreferenceHelper.getStringValueFromKey(
          SharedPreferenceKey.username);
      uploadRetailerDetailsMap[userMatchKey] = userSp;
    }
    // --> (3rd Process) Assigning value by key end:

    // --> (4rd Process) Assigning value by key start:
    // --> add {clientRetailerId : value} && {clientRetailerId : value} if not exit start
    String clientMatchKey = ErrorHandling.getContainsKey(
      detail: uploadRetailerDetailsMap,
      inputKey: ConstantName.clientRetailerId,
    );

    if (clientMatchKey.trim().isEmpty) {
      uploadRetailerDetailsMap[ConstantName.clientRetailerId] =
          retailerID.text;
      uploadRetailerDetailsMap[ConstantName.dependentClientRetailerId] =
          ConstantName.removeKeyValue;
    } else if (uploadRetailerDetailsMap[clientMatchKey] == null ||
        uploadRetailerDetailsMap[clientMatchKey].toString().trim().isEmpty) {
      uploadRetailerDetailsMap[clientMatchKey] = retailerID.text;
      uploadRetailerDetailsMap[ConstantName.dependentClientRetailerId] =
          ConstantName.clearValue;
    } else {
      uploadRetailerDetailsMap[ConstantName.dependentClientRetailerId] =
          ConstantName.dontRemove;
    }
    // --> add {clientRetailerId : value} if not exit end
    // --> (4rd Process) Assigning value by key end:

    if (uploadRetailerDetailsMap.isNotEmpty) {
      Map<String, String> uploadRetailerDetailsTableFields =
          getFieldTypes(uploadRetailerDetailsMap);
      if (uploadRetailerDetailsTableFields.isNotEmpty) {
        // --> To create table
        await DatabaseHelper.instance.createUploadRetailerIdTable(
            uploadRetailerIdTableFields: uploadRetailerDetailsTableFields);

        // --> Insert updateRetailerDetailsMap data
        await DatabaseHelper.instance.insertMultipleData(
            tableName: ConstantName.uploadRetailerIdTable,
            dataList: <Map<String, dynamic>>[uploadRetailerDetailsMap]);
      }
    }
  }

  // print('derived:<2> $derived');
  // print('derived:<2> ${derived.values.elementAt(derived.length - 2)}');

  return derived;
}