Dart 如何使用';其中';用飞镖';(动态)=>;动态';不是类型为';(类别)=>;布尔';属于';测试';
我正在使用search delegate,并尝试使用Dart 如何使用';其中';用飞镖';(动态)=>;动态';不是类型为';(类别)=>;布尔';属于';测试';,dart,Dart,我正在使用search delegate,并尝试使用where筛选出搜索结果 目前,开展了以下工作: suggestionList = snapshot.data .where((c) => c.number == query || c.words.contains(query)) .toList(); // can abstract this out in the future 我试图将布尔测试抽象
where
筛选出搜索结果
目前,开展了以下工作:
suggestionList = snapshot.data
.where((c) => c.number == query || c.words.contains(query))
.toList(); // can abstract this out in the future
我试图将布尔测试抽象到我的Chorus类本身,但这不起作用:
suggestionList = snapshot.data
.where((c) => c.isMatching(query))
.toList();
抽象就在这里
class Chorus {
bool isMatching(String query) {
bool isMatching = false;
var queryFormatted =
query.toLowerCase().replaceAll(new RegExp(r'[^\w\s]+'), '');
var wordsFormatted =
words.toLowerCase().replaceAll(new RegExp(r'[^\w\s]+'), '');
if (query == this.number) {
isMatching = true;
}
if (wordsFormatted.contains(queryFormatted)) {
isMatching = true;
}
return isMatching;
}
}
这就是错误:
通过将返回值转换为布尔值解决了此问题
suggestionList = snapshot.data
.where((c) => c.isMatching(query) as bool)
.toList();
如果有人能解释为什么需要强制转换它,我们将不胜感激。如果看不到代码的其余部分,很难说清楚,但您可能使用了
AsyncSnapshot
,但没有明确指定最终为AsyncSnapshot
的数据类型(例如AsyncSnapshot
)。稍后当您尝试使用.where()
时,因为snapshot.data
是动态的
,因此无法推断回调的参数类型和返回类型,因此也将假定它是动态函数(动态)
。