如何修复 profanity_filter 字符串转换错误
how to fix the profanity_filter string cast error
我正在尝试在 flutter 中使用 ProfanityFilter 来过滤评论内容中的不良词,这是一个字符串。 ProfanityFilter 有一个被审查的单词列表,我想将其与未包含在 LDNOOBW list 中的脏话列表一起传递。但是,由于内容是字符串,所以我使用了强制转换,然后出现强制转换错误:type 'ProfanityFilter' is not a subtype of type 'String' in type cast.
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 和编码非常陌生。
根据您在评论中的回复,您使用了错误的方法来执行此操作。你应该使用 .hasProfanity
.
方法
hasProfanity - 检测字符串是否包含亵渎语言
使用过滤器实例的 hasProfanity() 方法。传入要测试的字符串。
例子
final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.
如果你想得到字符串中坏词的列表,那么使用.getAllProfanity()
方法。
在您的代码中,'val' 将包含用户输入的字符串,这是您传递给方法的内容,而不是 'badString'。
我正在尝试在 flutter 中使用 ProfanityFilter 来过滤评论内容中的不良词,这是一个字符串。 ProfanityFilter 有一个被审查的单词列表,我想将其与未包含在 LDNOOBW list 中的脏话列表一起传递。但是,由于内容是字符串,所以我使用了强制转换,然后出现强制转换错误:type 'ProfanityFilter' is not a subtype of type 'String' in type cast.
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 和编码非常陌生。
根据您在评论中的回复,您使用了错误的方法来执行此操作。你应该使用 .hasProfanity
.
方法
hasProfanity - 检测字符串是否包含亵渎语言
使用过滤器实例的 hasProfanity() 方法。传入要测试的字符串。
例子
final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.
如果你想得到字符串中坏词的列表,那么使用.getAllProfanity()
方法。
在您的代码中,'val' 将包含用户输入的字符串,这是您传递给方法的内容,而不是 'badString'。