Flutter 里的String isEmpty 只是判断string是否为空,而不能判断string是否null,所以判断是否为空的前提,要判断str != null;不然,一旦字符串为null,调用isEmpty时就会出现以下exception,
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The getter 'isEmpty' was called on null.
同时这之后的所有代码将终止执行
这里不同于java里的字符串判断为空的方法 TextUtils.isEmpty(string):既判断是否为null,又判断是否为空。
下面时TextUtils里isEmpty方法的源码
public static boolean isEmpty(@Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
可能是由于Flutter里的isEmpty方法是来自String本身的类,所以判断的时候要提前判断字符串是否为null;而TextUtils是第三方类,所以可以直接判断传入的String是否为null