createEmbeddingModel method

  1. @override
Future<EmbeddingModel> createEmbeddingModel({
  1. String? modelPath,
  2. String? tokenizerPath,
  3. PreferredBackend? preferredBackend,
})
override

Creates and returns a new EmbeddingModel instance.

Modern API: If paths are not provided, uses the active embedding model set via FlutterGemma.installEmbedder() or modelManager.setActiveModel().

Legacy API: Provide explicit paths for backward compatibility.

modelPath — path to the embedding model file (optional if active model set). tokenizerPath — path to the tokenizer file (optional if active model set). preferredBackend — backend preference (e.g., CPU, GPU).

Implementation

@override
Future<EmbeddingModel> createEmbeddingModel({
  String? modelPath,
  String? tokenizerPath,
  PreferredBackend? preferredBackend,
}) {
  // FIRST statement, before every guard. It is idempotent and one-shot, so
  // it needs neither resolved paths nor cache state — and putting it in a
  // branch is what made it unreachable twice: once behind the singleton
  // cache, once behind "only on reuse". The ordinary shape is a single call
  // held for the app's lifetime; if it does not speak here it never speaks.
  noticeEmbedderBackendIgnored(preferredBackend);

  // Serialised, because resolving paths, comparing them against the cached
  // embedder and building are three steps with awaits between them: run
  // interleaved, two callers both finish deciding before either records a
  // model, and one of the two builds is orphaned with nobody to close it.
  return _embedderCache.serialize(
    () => _reuseOrBuildEmbedder(
      modelPath: modelPath,
      tokenizerPath: tokenizerPath,
      preferredBackend: preferredBackend,
    ),
  );
}