install method
Installs the model from the given source
This method performs the actual installation:
- NetworkSource: downloads from URL
- AssetSource: copies from Flutter assets
- BundledSource: accesses native resources
- FileSource: registers external file path
Parameters:
source: The model source to install fromcancelToken: Optional token for cancelling the installation
Throws:
- UnsupportedError if this handler doesn't support the source type
- ArgumentError if the source is invalid
- DownloadCancelledException if cancelled via cancelToken
- Platform-specific exceptions for download/file errors
Implementation
@override
Future<void> install(
ModelSource source, {
CancelToken? cancelToken,
}) async {
if (source is! AssetSource) {
throw ArgumentError('AssetSourceHandler only supports AssetSource');
}
final filename = path.basename(source.path);
final targetPath = await fileSystem.getTargetPath(filename);
// LargeFileHandler's `targetName` parameter is *just* a filename — the
// plugin prepends app docs dir itself. We keep the bare filename here.
// On platforms where large_file_handler doesn't ship a plugin (desktop:
// macOS/Windows/Linux, web stub) the channel call throws
// MissingPluginException — fall back to in-memory loadAsset → writeFile.
//
// Lookup keys differ between paths:
// - `pathForLookupKey` (no `assets/` prefix) for the native channel call
// - `normalizedPath` (with `assets/` prefix) for the Flutter rootBundle
// fallback (#250 Mode 2)
if (assetLoader is FlutterAssetLoader) {
try {
await (assetLoader as FlutterAssetLoader)
.copyAssetToFile(source.pathForLookupKey, filename);
} on MissingPluginException {
final assetData = await assetLoader.loadAsset(source.normalizedPath);
await fileSystem.writeFile(targetPath, assetData);
}
} else {
final assetData = await assetLoader.loadAsset(source.normalizedPath);
await fileSystem.writeFile(targetPath, assetData);
}
final sizeBytes = await fileSystem.getFileSize(targetPath);
final modelInfo = ModelInfo(
id: filename,
source: source,
installedAt: DateTime.now(),
sizeBytes: sizeBytes,
type: ModelType.inference,
hasLoraWeights: false,
);
await repository.saveModel(modelInfo);
}