Speech to text in a minute
Flutter implementation of speech-to-text functionality
Published in
2 min readAug 20, 2022
You want to add speech-to-text functionality in your app or just want to see how it’s done. Let’s see how to do it in flutter.
Step 1: Add the plugin
speech_to_text: <latest version>
Step 2: Initialise it
bool _speechEnabled = false;
String _lastWords = '';
final SpeechToText speechToText = SpeechToText();Future<void> initSpeech() async {
_speechEnabled = await speechToText.initialize();
}
Step 3: Write the start and stop function
///Call when you want to listen
void startListening() async {
await speechToText.listen(onResult: _onSpeechResult);
}///Call when you want to stop listening
void stopListening() async {
await speechToText.stop();
}///It automatically get called when plugin recognize any word.
void _onSpeechResult(SpeechRecognitionResult result) {
_lastWords = result.recognizedWords;//here you can do anything with recognize words.
}
Step 4: Check the status, if the plugin is enabled or not
String status() {if (speechEnabled) {
return “Press the button to start speaking”;
} else {
return “Speech not available”;
}}
That’s it, now you can directly use these functions with your UI class.
You can check this repository where I’m using this code.
See you guys in the next “in a minute” article.