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 this shell resolves paths from preferences before it
  // can compare them — an await between "decide" and "record" that two
  // callers can both slip through. Moving that resolution earlier once
  // widened exactly that window; the lane makes the ordering not matter.
  return _embedderCache.serialize(
    () => _reuseOrBuildEmbedder(
      modelPath: modelPath,
      tokenizerPath: tokenizerPath,
      preferredBackend: preferredBackend,
    ),
  );
}