Search code examples
filterflutterdartspeech-to-text

search with Text to speech works only either with speech or textField


So I have a normal TextField with which I filter a list children: products.map((doc) => _buildSingleProduct(doc)).toList(),, it works as it should, then I added to it a text-to-speech pluugin speech_recognition: and the combined it with the filtering function, It works all fine. The problem is when I finish with the speech filtering then I for example want to add or make corrections to it with writing to the TextField it doesn't filter anymore.

Textfield

              child: TextField(
                controller: controller,
                decoration: InputDecoration(
                  labelText: allTranslations.text(StringConstant.search),
                  prefixIcon: Icon(Icons.search),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.mic),
                    onPressed: () {
                      if (_isAvailable && !_isListening)
                        _speechRecognition
                            .listen(locale: "en_US")
                            .then((result) => print('$result'));
                    },
                  ),
                ),
              ),

As you can see there is the controller which I use to filter, and then the mic icon to pass the result from the speech to the controller like this:

 _speechRecognition
        .setRecognitionResultHandler((String result) => setState(() {
              controller = TextEditingController(text: resultText = result);
            }));

here I get the result from the speech and add it to the resultText of the filter and the controller so it appears in the textField.

if I do it like this:

   _speechRecognition
       .setRecognitionResultHandler((String speech) => setState(() => resultText = speech));

it works all fine but the text does not appear in the text-field obviously.

for the textField filtering I init the state to add it to the resultText:

  initState() {
    initSpeechRecognizer();
    controller.addListener(() {
      setState(() {
        resultText = controller.text;
      });
    });
    super.initState();
  }

this is how I return the result from the db:

return resultText == null || resultText == ""
        ? buildProducts(id, title, favorite, message, price, doc)
        : doc.data['title'].toLowerCase().contains(resultText.toLowerCase())
            ? buildProducts(id, title, favorite, message, price, doc)
            : Container();

as you can probably see I search the title.

So the problem one more time,

1.search with speech

  1. it appears on the textField and filters the list

  2. when I press the textField to change the query it doesn't filter anymore.

But the other way around works

  1. filter the list with text

    1. it filters the list

    2. I activate speech-to-text and it changes the query and filters the list with the new query.


Solution

  • So for people who need the solution

     _speechRecognition
            .setRecognitionResultHandler((String result) => setState(() {
                  resultText = result;
                  controller.text = resultText;
                }));
    

    you get the result from the speech, add this to the handling variable, but you also than add that result to the controller so you get the result at the textField.