简体   繁体   中英

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?

You don't need cascade notation there:

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

I forgot adding RegExp.

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

is working!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM