ServiceRegistry class
Dependency Injection Container for managing service lifecycle
Features:
- Singleton pattern - one instance per application
- Platform-aware service selection (web vs mobile)
- Lazy initialization of services
- Constructor injection for testability
- Clear separation of concerns
Platform Support:
- Mobile: Uses dart:io-based services (PlatformFileSystemService, BackgroundDownloaderService)
- Web: Uses URL-based services (WebFileSystemService, WebDownloadService)
Requirements:
- Mobile platforms require Flutter bindings to be initialized before ServiceRegistry (BackgroundDownloaderService uses path_provider which requires bindings)
- Call
WidgetsFlutterBinding.ensureInitialized()
in main() before using ServiceRegistry
Usage:
void main() {
WidgetsFlutterBinding.ensureInitialized(); // Required for mobile
ServiceRegistry.initialize();
runApp(MyApp());
}
// Later in your code:
final registry = ServiceRegistry.instance;
final handlerRegistry = registry.sourceHandlerRegistry;
final handler = handlerRegistry.getHandler(source);
await handler.install(source);
Properties
- assetHandler → SourceHandler
-
no setter
- assetLoader → AssetLoader
-
no setter
- bundledHandler → SourceHandler
-
no setter
- downloadService → DownloadService
-
no setter
- fileHandler → SourceHandler
-
no setter
- fileSystemService → FileSystemService
-
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- huggingFaceToken → String?
-
final
- maxDownloadRetries → int
-
final
- modelRepository → ModelRepository
-
no setter
- networkHandler → NetworkSourceHandler
-
no setter
- protectedFilesRegistry → ProtectedFilesRegistry
-
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sourceHandlerRegistry → SourceHandlerRegistry
-
no setter
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → ServiceRegistry
-
Gets the singleton instance
no setter
Static Methods
-
initialize(
{String? huggingFaceToken, int maxDownloadRetries = 10, FileSystemService? fileSystemService, AssetLoader? assetLoader, DownloadService? downloadService, ModelRepository? modelRepository, ProtectedFilesRegistry? protectedFilesRegistry}) → void - Initializes the singleton instance
-
reset(
) → void - Resets the singleton (primarily for testing)