【问题标题】:how to fix the profanity_filter string cast error如何修复 profanity_filter 字符串转换错误
【发布时间】:2021-10-12 20:16:21
【问题描述】:

我正在尝试在颤振中使用 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 和编码非常陌生。

【问题讨论】:

  • 您是在使用这个textformfield添加新的坏词,还是在检查输入到textformfield的字符串是否包含坏词?
  • 检查输入的字符串是否有坏词。

标签: string list flutter profanity


【解决方案1】:

根据您在 cmets 中的回复,您使用了错误的方法来执行此操作。你应该使用.hasProfanity

方法

hasProfanity - 检测字符串是否包含脏话

使用过滤器实例的 hasProfanity() 方法。传入要测试的字符串。

例子

final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.

如果您想获取字符串中的坏词列表,请使用.getAllProfanity() 方法。

在您的代码中,“val”将包含用户输入的字符串,这是您传递给方法的字符串,而不是“badString”。

【讨论】:

  • 哦...所以,如果我想掩盖评论中的所有脏话,我就通过:setState(() {content = filter.censor(val); viewModel.setDescription(content) ;});?.谢谢!试过了,它没有审查......
  • 我从来没有提到审查方法,我自己也不使用包。我只是在阅读和解释其自述文件中发布的包裹信息。我认为您需要更仔细地研究这些信息,也许可以查看 github repo 上的示例。恕我直言,这一切都解释得很清楚。
  • 我认为您现在的问题是您如何处理“内容”变量。我认为审查员已经奏效了。您可以通过在方法运行后将变量打印到控制台或使用调试器检查其值来测试它。
  • 它正在工作。只需要为某些单词添加一些 if 和 else 条件。谢谢!
  • 太好了,很高兴我能帮上忙。
猜你喜欢
  • 2023-04-07
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 2015-12-05
  • 2020-02-13
  • 2020-09-12
相关资源
最近更新 更多