Last active
May 11, 2021 11:05
-
-
Save leafney/f2bfd6d5d1c8c190597c35c759c21361 to your computer and use it in GitHub Desktop.
Dart String add isNullOrEmpty (https://dartpad.dev/f2bfd6d5d1c8c190597c35c759c21361?null_safety=true)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
String? ss; | |
print(ss); | |
// ss = " "; | |
// ss="hello"; | |
print(ss); | |
print('isNullOrEmpty: ${ss.isNullOrEmpty()}'); | |
print('isNotNullAndEmpty: ${ss.isNotNullAndEmpty()}'); | |
print('getStrValue: ${ss.getStrValue(val: "x")}'); | |
print('-------'); | |
print('StrUtils.isNullOrEmpty: ${StrUtils.isNullOrEmpty(ss)}'); | |
print('StrUtils.isNotNullAndEmpty: ${StrUtils.isNotNullAndEmpty(ss)}'); | |
print('StrUtils.getStrValue: ${StrUtils.getStrValue(ss)}'); | |
} | |
// use Extension | |
extension StringExtensions on String? { | |
/// string is null or empty | |
bool isNullOrEmpty() => this == null || this!.trim().isEmpty; | |
/// string not null and empty | |
bool isNotNullAndEmpty() => this != null && this!.trim().isNotEmpty; | |
/// if string is null or empty ,return default value | |
// String getNullOrEmptyVal({String val = ""}) { | |
// if(this.isNullOrEmpty()){ | |
// return val; | |
// } | |
// return this!; | |
// } | |
String getStrValue({String val = ""}) => this.isNullOrEmpty() ? val : this!; | |
} | |
// use Function | |
class StrUtils { | |
StrUtils._(); | |
/// string is null or empty | |
static bool isNullOrEmpty(String? s) => s == null || s.trim().isEmpty; | |
/// string not null and empty | |
static bool isNotNullAndEmpty(String? s) => s != null && s.trim().isNotEmpty; | |
/// if string is null or empty ,return default value | |
static String getStrValue(String? s, {String val = ""}) => | |
isNullOrEmpty(s) ? val : s!; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment