简体   繁体   English

为什么在 Dart 中执行方法级联时出现此错误?

[英]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!!正在工作中!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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