loadModelFromAssets method

Future<ExecuTorchModel> loadModelFromAssets(
  1. String assetPath
)

Loads a model from the Flutter asset bundle and caches it.

Equivalent to reading the asset yourself and calling ExecutorchManager.loadModelFromBytes.

Like loadModelFromAsset, the result is typed as the class this package exports under the name ExecuTorchModel — the core interface on native, the Wasm-backed class on web — so assigning it to an ExecuTorchModel variable compiles on every platform. The narrowing cast is what makes that possible: ExecutorchManager.loadModelFromBytes is declared in the core and can only promise the core interface, but on web every manager this package can hand you returns the web class.

Implementation

Future<impl.ExecuTorchModel> loadModelFromAssets(String assetPath) async {
  final byteData = await rootBundle.load(assetPath);
  final model = await loadModelFromBytes(byteData.buffer.asUint8List());
  // Redundant on native, where `impl.ExecuTorchModel` *is* the core
  // interface — which is the only branch the analyzer ever resolves. On web
  // it is a real downcast to this package's Wasm-backed class, and without
  // it this method does not compile there at all.
  // ignore: unnecessary_cast
  return model as impl.ExecuTorchModel;
}