getMessage static method

Map<String, dynamic> getMessage(
  1. Map<String, dynamic> json,
  2. bool isSSE
)

Implementation

static Map<String, dynamic> getMessage(
    Map<String, dynamic> json, bool isSSE) {
  String field = isSSE ? "data" : "answer";
  try {
    if (json[field] != null) {
      String fieldValue = json[field].toString().trim();
      // Check if it's a JSON string (starts with { or [)
      if (fieldValue.startsWith("{") || fieldValue.startsWith("[")) {
        dynamic decoded = jsonDecode(fieldValue);
        if (decoded is Map<String, dynamic>) {
          return decoded;
        } else {
          return {"message": decoded};
        }
      } else {
        return {"message": json[field]};
      }
    }
  } catch (e) {
    return {};
  }
  return {};
}