encode static method

String encode(
  1. String text
)

Implementation

static String encode(String text) {
  // print("text #${text}");
  var a = text.runes.toList();
  String textHexa;
  List<String> hexalist = [];
  a.forEach((element) {
    textHexa = element.toRadixString(16);
    if (textHexa.length > 4) {
      hexalist.add(textHexa);
    } else {
      var paddedString = textHexa.padLeft(4, '0');
      hexalist.add(paddedString);
    }
  });
  String texta;

  List<String> finalText = [];
  hexalist.forEach((element) {
    if (element.length > 4) {
      texta = r"\u" + element;
      finalText.add(texta);
      // checkListTextAllow(texta).then((value) {
      //   finalText.add(texta);
      // });
    } else {
      texta = r"\u" + element;
      List<int> hexnotAlowed = [12644];
      int? hexCheck;
      final Pattern unicodePattern = new RegExp(r'\\u([0-9A-Fa-f]{4})');
      final String newStr =
      texta.replaceAllMapped(unicodePattern, (Match unicodeMatch) {
        final int hexCode = int.parse(unicodeMatch.group(1)!, radix: 16);
        hexCheck = hexCode;
        final unicode = String.fromCharCode(hexCode);
        return unicode;
      });
      bool isAllowed = true;
      hexnotAlowed.forEach((element) {
        if (element == hexCheck!) {
          isAllowed = false;
        }
      });
      if (isAllowed) {
        finalText.add(newStr);
      } else {
        finalText.add("");
      }
    }
  });

  String joinString = finalText.join("");

  return joinString;
}