Reputation:
What are the possible ways to simplify null checks in dart code given below:
The code given below checks whether passed parameters are null or empty and assigns them to correct values.
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;
}
Upvotes: 1
Views: 1759
Reputation: 657148
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;
Upvotes: 4