首页 > 解决方案 > 如何基于包含 scheme+host+port 的字符串创建 URL

问题描述

我有一个包含方案+主机+端口的字符串。例如:http://192.120.123.1:4000方案在哪里http,主机是 192.120.123.1,端口是4000

其他例子是:

https://stackoverflow.com/

http:localhost

http://google.com:4000

如何基于该字符串创建 Dart的Uri类的实例?

标签: dart

解决方案


您可以使用Uri.parse将 a 解析String为 Uri,或使用命名参数初始化,Uri例如:

Uri(scheme: 'https', host: 'google.com', port: 4000);

推荐阅读