如何简化dart中的空检查
下面给出了简化dart代码中空检查的可能方法: 下面给出的代码检查传递的参数是null还是空,并将它们分配给正确的值如何简化dart中的空检查,dart,Dart,下面给出了简化dart代码中空检查的可能方法: 下面给出的代码检查传递的参数是null还是空,并将它们分配给正确的值 bool showBasicDialog = false; String startPath = ''; String frameToolID = ''; String path = ''; String host = ''; String frameToolName = ''; /// for opening a frame tool void openFrameTool(
bool showBasicDialog = false;
String startPath = '';
String frameToolID = '';
String path = '';
String host = '';
String frameToolName = '';
/// for opening a frame tool
void openFrameTool(
String frameToolNameInp,
String toolIDInp,
String pathInp,
String hostInp,
) async {
if (frameToolNameInp != null && frameToolNameInp.isNotEmpty) {
frameToolName = frameToolNameInp;
}
if (toolIDInp != null && toolIDInp.isNotEmpty) {
frameToolID = toolIDInp;
}
if (pathInp != null && pathInp.isNotEmpty) {
path = pathInp;
}
if (hostInp != null && hostInp.isNotEmpty) {
host = hostInp;
}
showBasicDialog = true;
}
在dart 2.10中,默认值为保留字,因此不能用作参数。但是,即使修复了这个问题,上面的函数也不会在dart中编译,并且会出现许多错误,包括
无法从函数“\u valueOrDefault”返回类型为“bool”的值,因为它的返回类型为“String”
。我弄错了吗?及:。感谢您的提示-已修复。在dart 2.10中,默认值是一个保留字,因此不能用作参数。但是,即使修复了这个问题,上面的函数也不会在dart中编译,并且会出现许多错误,包括无法从函数“\u valueOrDefault”返回类型为“bool”的值,因为它的返回类型为“String”
。我弄错了吗?及:。谢谢你的提示-修复了。
String _valueOrDefault(String value, String defaultValue) => (value?.isNotEmpty ?? false) ? value : defaultValue;
...
frameToolName = _valueOrDefault(frameToolNameInp, frameToolName);
frameToolID = _valueOrDefault(toolIDInp, frameToolID);
path = _valueOrDefault(pathInp, path);
host = _valueOrDefault(hostInp, host);
showBasicDialog = true;