uri解析不解析

Uri parse does not parse

  String baseUrl = "httpswwwmywebsiteit";
  Uri primaUrl = Uri.parse(baseUrl);
  print(primaUrl); // returns httpswwwmywebsiteit

这是一个有效的uri吗?如何发现格式错误的网址?例如,哪里有一个斜线而不是两个斜线?还是拼写错误的方案?

Irhn on "Uri.parse doesn't throw FormatException"

The Dart Uri class is not validating registered schemes, only the RFC 3986 grammar.
The only special caseing offered by the Uri class is recognition of the default port for the HTTP and HTTPS protocols.

你应该看这个:

字符串 "httpswwwmywebsiteit" 是一个有效的 URI 引用(根据 RFC 3986)。

这是一个带有 path-noscheme 的 relative-part,它只是一个 URI 路径段。

如果您想验证某个东西是有效的 URI,并且具有方案和路径,则需要进行检查。

if (!primaUrl.hasScheme || !primarUrl.hasAuthority || primaUrl.path.isEmpty) {
  // Or whichever requirements you have on the URI.
  throw ArgumentError.value(baseUrl, "baseUrl", "Not a full URI");
}

检查方案是否有效需要您定义其含义,然后您可以检查primaUrl.scheme是否满足。