isUrl function

bool isUrl(
  1. String str, {
  2. Set<String>? allowedSchemes,
  3. bool requireHost = true,
})

Returns true if str is a valid URL.

Implementation

bool isUrl(String str, {Set<String>? allowedSchemes, bool requireHost = true}) {
  final trimmed = str.trim();
  if (trimmed.isEmpty) return false;

  final uri = Uri.tryParse(trimmed);
  if (uri == null || !uri.hasScheme) return false;

  final scheme = uri.scheme.toLowerCase();
  if (allowedSchemes != null && !allowedSchemes.contains(scheme)) {
    return false;
  }

  if (requireHost && uri.host.isEmpty) return false;

  return true;
}