Reputation: 328
I have a single line code which removes trailing and leading whitespaces, and also replaces multiple spaces in between with a single space. (from a string)
value = value..trim()..split(" +")..join(" ");
However I am getting the following error.
The method 'join' isn't defined for the type 'String'.
Try correcting the name to the name of an existing method, or defining a method named 'join'.(dartundefined_method)
What i am doing wrong?
Upvotes: 0
Views: 389
Reputation: 328
I forgot adding RegExp.
value = value.trim().split(RegExp(" +")).join(" ");
is working!!
Upvotes: 0
Reputation: 3443
You don't need cascade notation there:
value = value.split(' ').where((x) => x.isNotEmpty).map((x) => x.trim()).join(" ")
Upvotes: 1