-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Use case
Currently this 4 methods (https://github.com/flutter/flutter/blob/flutter-2.13-candidate.0/packages/flutter_tools/lib/src/runner/flutter_command.dart#L1505) checks if argResults is null and it defaults to dummy values if so. They make an assumption that the argument passed to them is valid therefore if the key is not found this methods raise an ArgumentError
/// Gets the parsed command-line option named [name] as a bool.
bool boolArg(String name) => argResults?[name] as bool? ?? false;
/// Gets the parsed command-line option named [name] as a String.
String? stringArg(String name) => argResults?[name] as String?;
/// Gets the parsed command-line option named [name] as an int.
int? intArg(String name) => argResults?[name] as int?;
/// Gets the parsed command-line option named [name] as List<String>.
List<String> stringsArg(String name) => argResults?[name] as List<String>? ?? <String>[];
Proposal
Make new boolArg and stringArg functions to return bool? and List? respectively and for stringArg and intArg to check if the given argument is valid to avoid the ArgumentError; then start incrementally using the new functions in all the places needed; the caller should handle what default value to use if a null is returned.
bool? boolArgSafe(String name) {
if (argResult == null) {
return null
} else {
bool? result;
if (argParser.options.containsKey(name)) {
result = argResults[name] as bool? ?? false
}
return result;
}
}
// eg: argResults = {"key1": bool}
void myCaller(){
bool? key1 = boolArg("key1")
bool? key2 = boolArg("key2")
if (key1 == null || key2== null){
print("Missing params")
}
...
}
- rename current functions
fnNametofnNameDeprecated - make new function handling nulls
bool? boolArg - make new function handling nulls
String? stringArg - make new function handling nulls
int? intArg - migrate
boolArgDeprecatedtoboolArg - migrate
stringArgDeprecatedtostringArg - migrate
intArgDeprecatedtointArg - rename
stringsArgDeprecatedtostringsArg