installWithProgress method

  1. @override
Stream<int> installWithProgress(
  1. ModelSource source, {
  2. CancelToken? cancelToken,
})
override

Installs the model with progress tracking

Returns a stream of progress percentages (0-100)

Parameters:

  • source: The model source to install from
  • cancelToken: Optional token for cancelling the installation

Note: Some sources may not support true progress:

  • AssetSource: simulates progress (copy is instant)
  • BundledSource: returns 100 immediately (no download)
  • FileSource: returns 100 immediately (just registration)

Example:

final cancelToken = CancelToken();

try {
  await for (final progress in handler.installWithProgress(
    source,
    cancelToken: cancelToken,
  )) {
    print('Progress: $progress%');
  }
} catch (e) {
  if (CancelToken.isCancel(e)) {
    print('Installation cancelled');
  }
}

Throws:

Implementation

@override
Stream<int> installWithProgress(
  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);

  if (assetLoader is FlutterAssetLoader) {
    try {
      await for (final progress in (assetLoader as FlutterAssetLoader)
          .copyAssetToFileWithProgress(source.pathForLookupKey, filename)) {
        yield progress;
      }
    } on MissingPluginException {
      final assetData = await assetLoader.loadAsset(source.normalizedPath);
      await fileSystem.writeFile(targetPath, assetData);
      yield 100;
    }
  } else {
    final assetData = await assetLoader.loadAsset(source.normalizedPath);
    await fileSystem.writeFile(targetPath, assetData);
    yield 100;
  }

  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);
}