Flutter
Model Management API Reference
The Model Management API provides fine-grained control over OCR model lifecycle in the Flutter Vision SDK. This document provides comprehensive API documentation for all available methods, classes, and enums.
Table of Contents
- Overview
- Getting Started
- Core Classes
- API Methods
- Data Models
- Enums
- Exceptions
- Lifecycle Events
- Usage Examples
- Platform Differences
Overview
The Model Management API enables:
- Pre-downloading models during app onboarding for offline-first UX
- Explicit memory management by loading/unloading models on demand
- Background updates by checking for model updates before downloading
- Fine-grained control over model lifecycle with detailed state queries
- Progress tracking for long-running download operations
- Lifecycle monitoring via listener callbacks
Key Benefits
- Reduced app initialization time: Pre-download models during onboarding
- Memory efficiency: Load only the models you need, when you need them
- Better UX: Show download progress, cancel operations, handle errors gracefully
- Offline-first: Download models once, use them indefinitely without network
- Update management: Check for updates before downloading to avoid unnecessary bandwidth usage
Getting Started
Import the Package
Initialize ModelManager
IMPORTANT: ModelManager must be initialized before any other API calls.
Parameters
Returns
Future<void>- Completes when initialization is successful
Throws
ModelSdkNotInitializedException- If VisionSDK is not initialized firstModelException- For other initialization errors
Builder Methods
Core Classes
ModelManager
Singleton class for managing OCR model lifecycle.
ModelManagerBuilder
Builder for configuring ModelManager initialization.
OCRModule
Represents a specific model configuration (class + size).
API Methods
downloadModel
Download a model to device storage without loading it into memory.
Parameters
* Either apiKey or token is required
Returns
Future<void>- Completes when download finishes
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNoNetworkException- No network connectionModelNetworkException- Network error during downloadModelStorageException- Insufficient storage or write errorModelAlreadyDownloadedException- Model already downloadedModelException- Authentication or other errors
Example
cancelDownload
Cancel an ongoing model download.
Parameters
Returns
Future<void>- Completes when cancellation is processed
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNotFoundException- No active download for this modelModelException- Other errors
Example
loadModel
Load a downloaded model into memory for inference.
Parameters
* Either apiKey or token is required
Returns
Future<void>- Completes when model is loaded
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNotFoundException- Model not downloadedModelLoadException- Failed to load modelModelAlreadyLoadedException- Model already loadedModelException- Authentication or other errors
Example
unloadModel
Unload a model from memory to free resources.
Parameters
Returns
Future<void>- Completes when model is unloaded
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNotFoundException- Model not loadedModelException- Other errors
Example
deleteModel
Delete a downloaded model from device storage.
Parameters
Returns
Future<void>- Completes when model is deleted
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNotFoundException- Model not downloadedModelException- Other errors
Notes
- If the model is loaded, it will be unloaded before deletion
- This operation is irreversible
Example
isModelLoaded
Check if a model is currently loaded in memory.
Parameters
Returns
Future<bool>-trueif loaded,falseotherwise
Throws
ModelSdkNotInitializedException- ModelManager not initialized
Example
loadedModelCount
Get the number of models currently loaded in memory.
Returns
Future<int>- Number of loaded models
Throws
ModelSdkNotInitializedException- ModelManager not initialized
Example
findLoadedModels
Get detailed information about all loaded models.
Returns
Future<List<ModelInfo>>- List of loaded model information
Throws
ModelSdkNotInitializedException- ModelManager not initialized
Example
findDownloadedModels
Get detailed information about all downloaded models.
Returns
Future<List<ModelInfo>>- List of downloaded model information
Throws
ModelSdkNotInitializedException- ModelManager not initialized
Example
findDownloadedModel
Get information about a specific downloaded model.
Parameters
Returns
Future<ModelInfo?>- Model information, ornullif not downloaded
Throws
ModelSdkNotInitializedException- ModelManager not initialized
Example
checkModelUpdates
Check if a newer version of a model is available.
Parameters
* Either apiKey or token is required
Returns
Future<ModelUpdateInfo>- Update availability information
Throws
ModelSdkNotInitializedException- ModelManager not initializedModelNetworkException- Network errorModelException- Authentication or other errors
Example
Data Models
ModelInfo
Detailed information about a model.
Fields
DownloadProgress
Progress information for model downloads.
Fields
Convenience Properties
ModelUpdateInfo
Information about model update availability.
Fields
Enums
ModelClass
Represents the type of OCR model.
Values
ModelSize
Represents the size variant of a model.
Values
Compatibility Matrix
ExecutionProvider
Android only: Execution provider for model inference.
Values
Notes
- Only used on Android; ignored on iOS
nnapiprovides hardware acceleration on supported devicesxnnpackis recommended for best mobile performance- Defaults to
cpuif not specified
Exceptions
All exceptions extend ModelException base class.
Exception Hierarchy
ModelException
Base exception class for all model-related errors.
ModelSdkNotInitializedException
Thrown when attempting to use ModelManager before initialization.
When Thrown
- Any ModelManager method called before
initialize() - VisionSDK not initialized before ModelManager
Solution
ModelNoNetworkException
Thrown when network is unavailable for operations requiring connectivity.
When Thrown
downloadModel()called without networkcheckModelUpdates()called without network
Solution
- Check network connectivity before calling
- Show user-friendly error message
- Retry when network is available
ModelNetworkException
Thrown for network-related errors during download or API calls.
When Thrown
- Download fails due to server error
- API authentication fails
- Timeout during download
- Invalid API key or token
Common Causes
- Invalid API key or token
- Server maintenance
- Slow/unstable connection
- Rate limiting
ModelStorageException
Thrown for storage-related errors.
When Thrown
- Insufficient storage space
- File system write error
- Storage permissions denied
Solution
- Check available storage before download
- Request storage permissions
- Free up space
ModelNotFoundException
Thrown when attempting to operate on a non-existent model.
When Thrown
loadModel()on model not downloadedunloadModel()on model not loadeddeleteModel()on model not downloadedcancelDownload()with no active download
ModelAlreadyDownloadedException
Thrown when attempting to download an already-downloaded model.
When Thrown
downloadModel()on already-downloaded model
Solution
- Check with
findDownloadedModel()before downloading - Delete old version before re-downloading
ModelAlreadyLoadedException
Thrown when attempting to load an already-loaded model.
When Thrown
loadModel()on already-loaded model
Solution
- Check with
isModelLoaded()before loading
ModelLoadException
Thrown when model loading fails.
When Thrown
- Model files corrupted
- Incompatible model format
- Insufficient memory
- Invalid execution provider (Android)
Solution
- Delete and re-download model
- Free up memory
- Try different execution provider (Android)
Lifecycle Events
Monitor model lifecycle events using ModelLifecycleListener.
ModelLifecycleListener
Abstract base class for lifecycle callbacks.
Event: onDownloadStarted
Called when a model download begins.
Parameters
Example
Event: onDownloadProgress
Called periodically during download with progress updates.
Parameters
Example
Event: onDownloadCompleted
Called when a model download completes successfully.
Parameters
Example
Event: onDownloadFailed
Called when a model download fails.
Parameters
Example
Event: onDownloadCancelled
Called when a model download is cancelled.
Parameters
Example
Event: onModelLoaded
Called when a model is successfully loaded into memory.
Parameters
Example
Event: onModelUnloaded
Called when a model is unloaded from memory.
Parameters
Example
Event: onModelDeleted
Called when a model is deleted from storage.
Parameters
Example
ModelLifecycleCallbacks
Convenience class for selective event handling using callbacks.
Example
Usage Examples
Complete Workflow Example
Onboarding Flow Example
Pre-download models during app onboarding for offline-first UX.
Background Update Check Example
Check for and download model updates in the background.
Platform Differences
iOS vs Android Feature Parity
Best Practices
1. Initialize Early
2. Check Before Operating
3. Handle Errors Gracefully
4. Unload Unused Models
5. Pre-download for Offline
6. Check for Updates Periodically
7. Use Lifecycle Listener
Troubleshooting
Model Not Found After Download
Symptom: ModelNotFoundException when trying to load after successful download.
Solution:
Download Hangs or Never Completes
Symptom: Download progress stops or lifecycle events not firing.
Solution:
- Check network connection
- Cancel and retry:
Memory Issues When Loading Multiple Models
Symptom: App crashes or ModelLoadException due to insufficient memory.
Solution:
Authentication Errors
Symptom: ModelNetworkException with "Invalid API key" or "Unauthorized".
Solution:
- Verify API key is correct
- Check environment (sandbox vs production)
- Use token instead of API key: