getActiveEmbedder static method
Get the active embedding model as a ready-to-use EmbeddingModel
Returns an EmbeddingModel configured with runtime parameters. The model and tokenizer paths come from the active EmbeddingModelSpec.
Runtime parameters:
preferredBackend: accepted for symmetry with getActiveModel, and not applied. On native every backend runs the forward pass on CPU: LiteRT's GPU delegate returns all-zero vectors for EmbeddingGemma's int4 weights, and the ONNX client appends no execution provider. On web the runtime chooses for itself and the argument is equally ignored. Passing anything other than PreferredBackend.cpu logs a line once per isolate in debug builds; read EmbeddingModel.activeBackend for the backend in use, which is available in release builds too.
Throws:
- StateError if no active embedding model is set
Example:
// Install embedding model first
await FlutterGemma.installEmbedder()
.modelFromNetwork('https://example.com/model.tflite')
.tokenizerFromNetwork('https://example.com/tokenizer.model')
.install();
final embedder = await FlutterGemma.getActiveEmbedder();
// Where the backend matters, ask — do not assume the argument took.
// Native answers PreferredBackend.cpu; web answers null, because there
// the accelerator is not known until the first embedding.
debugPrint('embedding on ${embedder.activeBackend}');
Implementation
static Future<EmbeddingModel> getActiveEmbedder({
PreferredBackend? preferredBackend,
}) async {
final manager = FlutterGemmaPlugin.instance.modelManager;
final activeSpec = manager.activeEmbeddingModel;
if (activeSpec == null) {
throw StateError(
'No active embedding model set. Use FlutterGemma.installEmbedder() first.',
);
}
if (activeSpec is! EmbeddingModelSpec) {
throw StateError(
'Active model is not an EmbeddingModelSpec. '
'Expected EmbeddingModelSpec, got ${activeSpec.runtimeType}',
);
}
// Create EmbeddingModel using active spec (paths resolved automatically)
return await FlutterGemmaPlugin.instance.createEmbeddingModel(
preferredBackend: preferredBackend,
);
}