in Dart, there's a convenient way to convert a String to an int:
int i = int.parse('123');
is there something similar for converting bools?
bool b = bool.parse('true');
Bool has no methods.
var val = 'True';
bool b = val.toLowerCase() == 'true';
should be easy enough.
With recent Dart versions with extension method support the code could be made look more like for int
, num
, float
.
extension BoolParsing on String {
bool parseBool() {
return this.toLowerCase() == 'true';
}
}
void main() {
bool b = 'tRuE'.parseBool();
print('${b.runtimeType} - $b');
}