getActiveStt static method
Get the active STT model as a ready-to-use SpeechRecognizer
Returns a SpeechRecognizer configured with runtime parameters. The model and tokenizer paths come from the active SttModelSpec.
Runtime parameters:
preferredBackend: CPU or GPU preference (optional)language: the OUTPUT language for transcripts (Whisper only) — a bare lowercase code such as'en'or'de'. Sets SpeechRecognizer.language, and retargets the recognizer if one already exists, so it takes effect on every call and not just the first. Override a single transcription with SpeechRecognizer.transcribe's ownlanguageinstead.
Throws:
- StateError if no active STT model is set
- ArgumentError for a malformed
language, or for anylanguageon a model whose decoder prompt has no language token (moonshine, parakeet)
Example:
// Install STT model first
await FlutterGemma.installStt()
.modelFromNetwork('https://example.com/model.tflite')
.tokenizerFromNetwork('https://example.com/tokenizer.json')
.ofType(SttModelType.moonshine)
.install();
// Create with default backend
final recognizer = await FlutterGemma.getActiveStt();
// Whisper: transcribe German, then French, on the SAME recognizer —
// nothing is reloaded between the two.
final de = await FlutterGemma.getActiveStt(language: 'de');
final german = await de.transcribe(germanPcm);
final french = await de.transcribe(frenchPcm, language: 'fr');
Implementation
static Future<SpeechRecognizer> getActiveStt({
PreferredBackend? preferredBackend,
String? language,
}) async {
final manager = FlutterGemmaPlugin.instance.modelManager;
final activeSpec = manager.activeSttModel;
if (activeSpec == null) {
throw StateError(
'No active STT model set. Use FlutterGemma.installStt() first.',
);
}
if (activeSpec is! SttModelSpec) {
throw StateError(
'Active model is not an SttModelSpec. '
'Expected SttModelSpec, got ${activeSpec.runtimeType}',
);
}
// Create SpeechRecognizer using active spec (paths resolved automatically)
return await FlutterGemmaPlugin.instance.createSttModel(
preferredBackend: preferredBackend,
language: language,
);
}