我正在尝试在flutter中使用ProfanityFilter,来过滤评论内容中的坏词,这是一个字符串。ProfanityFilter有一个被审查的单词列表,我想把它和LDNOOBW list中没有的脏话列表一起传递。但是,因为内容是字符串,所以我使用了强制转换,然后我得到了强制转换错误:类型'ProfanityFilter‘不是类型强制转换中的类型' string’的子类型。
TextFormField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.comment_outlined, color:
Colors.white60, size: 20,),
hintText: Languages
.of(context)
.revHint,
focusedBorder: UnderlineInputBorder(),
),
maxLines: null,
onChanged: (val) {
setState(() {
val = ProfanityFilter.filterAdditionally(badString) as String;
content = val;
viewModel.setDescription(content);
});
}
),如何将字符串传递给列表,扫描列表,然后将经过审查的列表解析为字符串?或者,有没有更好、更简单的方法来审查不好的词?也许是一张地图?谢谢!对flutter和编码非常陌生。
发布于 2021-08-10 06:24:41
根据您在评论中的回复,您使用了错误的方法来执行此操作。您应该使用.hasProfanity。
方法
hasProfanity -检测字符串是否包含亵渎内容
使用filter实例的hasProfanity()方法。传入要测试的字符串。
示例
final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.如果想要获取字符串中的坏词列表,请使用.getAllProfanity()方法。
在您的代码中,'val‘将包含用户输入的字符串,这是您传递给方法的内容,而不是'badString’。
https://stackoverflow.com/questions/68706936
复制相似问题