首页 > 解决方案 > 如何编写扩展方法来检查空白飞镖

问题描述

任何人都可以解释一下如何检查空白。

extension StringExtensions on String {

  bool isNullOrEmpty() => this == null || isEmpty;

 // check empty of white spaces


}

谢谢

标签: flutterdartextension-methods

解决方案


使用contains方法,或者你可以用正则表达式检查它,这里是一个例子

extension StringExtensions on String {
  bool get isNullOrEmpty {
    bool _hasSpace = RegExp(r'\s').hasMatch(this);
    //  bool _hasSpace = this.contains(' ');

    return this == null || isEmpty || _hasSpace;
  }
}

推荐阅读