stringlistflutterprofanity

how to fix the profanity_filter string cast error


I am trying to use ProfanityFilter in flutter, to filter bad words in a review content, which is a string. ProfanityFilter has a list of censored words, which I want to pass along with the list of swear words not included in a LDNOOBW list. However, since the content is a string, I use a cast, and then I get cast error: 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);
              });
            }
          ),

How do I pass string to a list, scan and then parse the censored list to a string back? Or is there a better and easier way to censore bad words? A map maybe? Thank you! Very new to flutter and coding.


Solution

  • Based on your response in comments, you are using the wrong method to do this. You should be using .hasProfanity.

    Methods

    hasProfanity - Detect if a string contains profanity

    Use the hasProfanity() method of the filter instance. Pass in the string to be tested.

    Example

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

    If you want to get a list of the bad words in the string then use the .getAllProfanity() method.

    In your code, 'val' will contain the string entered by the user and this is what you pass to the method, not 'badString'.