gpt4 book ai didi

dart - 如何简化Dart中的空检查

转载 作者:行者123 更新时间:2023-12-03 03:17:38 25 4
gpt4 key购买 nike

下面提供的简化Dart代码中的空检查的可能方法有哪些:

下面给出的代码检查传递的参数是否为空或为空,并将其分配给正确的值。

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;
}

最佳答案

  String _valueOrDefault(String value, String default) => (value?.isNotEmpty ?? false) ? value : default;

...

frameToolName = _valueOrDefault(frameToolNameInp, frameToolName);

frameToolID = _valueOrDefault(toolIDInp, frameToolID);

path = _valueOrDefault(pathInp, path);

host = _valueOrDefault(hostInp, host);

showBasicDialog = true;

关于dart - 如何简化Dart中的空检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52773058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com