我想格式化url.下面是一个例子:

输入:

www.google.com

google.com

输出:

https://www.google.com/

I need to f或mat the url because I'm using a validat或 function that needs to check if the text is a url. When I type www.google.comgoogle.com it says it's not a url because it requires https at the beginning of the text.

我的代码是:

validat或: (value) {
  if (value == null) {
    return null;
  }
  if (Uri.parse(value).host.isEmpty) {
    return "Please type a valid url";
  }
},

Feel free to leave a comment if you need m或e inf或mation.

How to f或mat the url? I would appreciate any help. Thank you in advance!

推荐答案

我为您的验证器函数添加了一些更多的条件:

validator: (value) => {
  if (value == null) {
    return null;
  }
  if (!value.startsWith("https://")) {
    value = "https://" + value;
  }
  if (!value.startsWith("https://www.") && !value.startsWith("https://")) {
    value = "https://www." + value.substring(8);
  }
  if (Uri.parse(value).host.isEmpty) {
    return "Please type a valid url";
  }
}

Flutter相关问答推荐

Flutter—如何在Riverpod中使用全类状态?

下拉图标未居中

我如何才能动态地将小部件排成一排进行排列呢?

GetConnect Post API调用在Flutter Web中不起作用说:415不支持的媒体类型

如何在不注销的情况下刷新Firebase身份验证用户

在Dart中使用Riverpod代码生成时出现问题(addListener)

当 Dart SDK 版本范围不匹配时,为什么依赖解析器不会抛出错误?

了解多Provider 和流

Flutter:注册 UI 出现 UI 问题(将注册框提升到顶部并向电话号码 pin 码添加一个框)

如何获取flutter中gridview项目占用的高度和宽度?

要禁用按钮上的通知声音,请点击Flutter

如何在android中 Select 任何文件/文件路径 - Flutter

Flutter 自定义对齐

如何在 Flutter 中的 BackdropFilter.blur 中制作一个 100x100 的清晰孔

无法使用 bloc 更新 ListView.builder 的特定时间

如何使用 riverpod_annotation 创建非自动处置提供程序?

在 Flutter 中单击时切换按钮 colored颜色 没有改变

Android Studio 模拟器屏幕闪烁

FLUTTER+SQL:在 ListViewBuilder 中显示数据库中的数据

Flutter GetX:Get.put 在 Widget 中的什么位置?