I know that this question has pretty good answers, but I want to show a fancy way to remove all whitespace in a string. I actually thought that Dart should've had a built-in method to handle this, so I created the following extension for String class:
extension ExtendedString on String {
/// The string without any whitespace.
String removeAllWhitespace() {
// Remove all white space.
return this.replaceAll(RegExp(r"\s+"), "");
}
}
Now, you can use it in a very simple and neat way:
String product = "COCA COLA";
print('Product id is: ${product.removeAllWhitespace()}');