Flutter TTS plugin not initializing on android through an async handler
Hi, I am getting this issue when i try to initialize the plugin through an async task.
E/flutter (11121): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method getLanguages on channel flutter_tts)
E/flutter (11121): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
E/flutter (11121):
I currently have a handler listening for action process text event on android which allows me to share the text selected in other android apps with my flutter app. Here is my code -
class _MyAppState extends State<MyApp> { FlutterTts flutterTts; dynamic languages; String language; double volume = 0.5; double pitch = 1.0; double rate = 0.5; bool isCurrentLanguageInstalled = false;
String _newVoiceText;
TtsState ttsState = TtsState.stopped;
get isPlaying => ttsState == TtsState.playing; get isStopped => ttsState == TtsState.stopped; get isPaused => ttsState == TtsState.paused; get isContinued => ttsState == TtsState.continued;
bool get isIOS => !kIsWeb && Platform.isIOS; bool get isAndroid => !kIsWeb && Platform.isAndroid; bool get isWeb => kIsWeb;
String inputText = '';
Future
@override initState() { super.initState(); initTts(); }
initTts() { flutterTts = FlutterTts();
_getLanguages();
if (isAndroid) {
_getEngines();
}
flutterTts.setStartHandler(() {
setState(() {
print("Playing");
ttsState = TtsState.playing;
});
});
flutterTts.setCompletionHandler(() {
setState(() {
print("Complete");
ttsState = TtsState.stopped;
});
});
flutterTts.setCancelHandler(() {
setState(() {
print("Cancel");
ttsState = TtsState.stopped;
});
});
if (isWeb || isIOS) {
flutterTts.setPauseHandler(() {
setState(() {
print("Paused");
ttsState = TtsState.paused;
});
});
flutterTts.setContinueHandler(() {
setState(() {
print("Continued");
ttsState = TtsState.continued;
});
});
}
flutterTts.setErrorHandler((msg) {
setState(() {
print("error: $msg");
ttsState = TtsState.stopped;
});
});
}
Future _getLanguages() async { languages = await flutterTts.getLanguages; if (languages != null) setState(() => languages); }
Future _getEngines() async { var engines = await flutterTts.getEngines; if (engines != null) { for (dynamic engine in engines) { print(engine); } } }
Any help would be appreciated.
Try to set languages in a FutureBuilder. I updated the example with something similar.