isIP method

bool isIP(
  1. String? str,
  2. int version
)

Check if the string str is IP version 4 or 6.

  • version is a String or an int.

Implementation

bool isIP(String? str, int version) {
  if (version != 4 && version != 6) {
    return isIP(str, 4) || isIP(str, 6);
  } else if (version == 4) {
    if (regex != null) {
      return regex!.hasMatch(str!);
    } else if (!_ipv4Maybe.hasMatch(str!)) {
      return false;
    }
    final List<String> parts = str.split('.')
      ..sort((String a, String b) => int.parse(a) - int.parse(b));
    return int.parse(parts[3]) <= 255;
  } else if (regex != null) {
    return regex!.hasMatch(str!);
  }
  return version == 6 && _ipv6.hasMatch(str!);
}