Aadarsh Patel
Aadarsh Patel

Reputation: 328

Why I am getting this error while doing Method Cascade in Dart?

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

Answers (2)

Aadarsh Patel
Aadarsh Patel

Reputation: 328

I forgot adding RegExp.

value = value.trim().split(RegExp(" +")).join(" ");

is working!!

Upvotes: 0

Kitsu
Kitsu

Reputation: 3443

You don't need cascade notation there:

value = value.split(' ').where((x) => x.isNotEmpty).map((x) => x.trim()).join(" ")

Upvotes: 1

Related Questions