By default The text to speech engine uses an async call, this causes the main Dart thread to go on with whatever line of code comes next, this leads up to another setstate() call after the TTS speak function which reverts the animation state from Speaking to Idle almost instantly.
is there any way to avoid this issue?
I want it to work as follows:
setstate(() => _animation = 'Speaking')
wait until the TTS is finished speaking
setstate(() => _animation = 'Idle')
If I'm misunderstanding something, could you please point it out? in case this is how Dart works then if you have any workarounds that'd be great.
Simplified code for inspection:
void _speak(String sentence) async {
updateRiveRoot(_animation = 'Speak');
setState(() => _isSpeaking = true);
if (!_isSpeaking) {
await tts.awaitSpeakCompletion(true);
tts.speak(sentence).then((result) {
if (result != null) {
print("getting here");// not being preinted out
setState(() => _isListening = false);
setState(() => _isSpeaking = false);
updateRiveRoot(_animation = 'Idle');
}
});
} else {
setState(() => _isSpeaking = true);
}
print("isSpeaking is $_isSpeaking and animation is $_animation");
}
I have scrolled through the TTS documentation but couldn't find useful information on the problem I'm currently facing.