singleton_manager 0.2.0
singleton_manager: ^0.2.0 copied to clipboard
A high-performance singleton manager for Dart with zero dependencies, type-safe generics support, lazy loading, and optional scope management. Ideal for dependency injection, service locators, and res [...]
singleton_manager #
A high-performance, zero-dependency singleton manager for Dart.
Features #
- Type-safe: Full generic support with compile-time type checking
- High performance: O(1) registration and retrieval operations
- Zero dependencies: No external package dependencies
- Dependency Injection: Factory-based DI with
SingletonDI(v0.2.0+) - Lifecycle Management:
ISingletoninterface for initialization and cleanup - Lazy loading: Initialize singletons only when first accessed
- Multi-platform: Supports VM, Web, Native, and Flutter
- Pure Dart: No platform-specific code required
Installation #
Add to your pubspec.yaml:
dependencies:
singleton_manager: ^0.2.0
Quick Start #
Dependency Injection (v0.2.0+) #
import 'package:singleton_manager/singleton_manager.dart';
// 1. Define your service
class UserService implements ISingleton<dynamic, void> {
@override
Future<void> initialize(dynamic input) async => print('init');
@override
Future<void> initializeDI() async => print('di init');
}
void main() async {
// 2. Register factory
SingletonDI.registerFactory<UserService>(UserService.new);
// 3. Add to manager
final manager = SingletonManager.instance;
await manager.add<UserService>();
// 4. Use it
final service = manager.get<UserService>();
}
Basic Usage #
import 'package:singleton_manager/singleton_manager.dart';
void main() {
// Create a manager
final manager = SingletonManager<String>();
// Register a singleton
manager.register('myService', () => MyService());
// Retrieve it (same instance always)
final service = manager.get('myService'); // Returns MyService
final serviceSame = manager.get('myService'); // Same instance
}
class MyService {
void doSomething() => print('Hello from Service!');
}
Lazy Loading #
// Singleton only created when first accessed
manager.registerLazy('expensiveService',
() => ExpensiveResourceService()
);
Documentation #
For more information, see the main project documentation.
Contributing #
See CONTRIBUTING.md for contribution guidelines.
License #
MIT License - see LICENSE file for details.