{"id":28721778,"url":"https://github.com/stefalda/inject_x","last_synced_at":"2026-04-25T11:31:57.652Z","repository":{"id":276994223,"uuid":"930979491","full_name":"stefalda/inject_x","owner":"stefalda","description":"A lightweight, easy-to-use service locator implementation for Dart applications. This package provides a simple dependency injection container that helps manage application dependencies with minimal setup.","archived":false,"fork":false,"pushed_at":"2025-06-08T13:46:12.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-08T14:30:48.909Z","etag":null,"topics":["dart","dartlang","dependency-injection","flutter","library"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stefalda.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-11T14:27:19.000Z","updated_at":"2025-06-08T13:46:16.000Z","dependencies_parsed_at":"2025-06-08T14:35:52.200Z","dependency_job_id":null,"html_url":"https://github.com/stefalda/inject_x","commit_stats":null,"previous_names":["stefalda/inject_x"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/stefalda/inject_x","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefalda%2Finject_x","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefalda%2Finject_x/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefalda%2Finject_x/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefalda%2Finject_x/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stefalda","download_url":"https://codeload.github.com/stefalda/inject_x/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefalda%2Finject_x/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32261021,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-25T09:15:33.318Z","status":"ssl_error","status_checked_at":"2026-04-25T09:15:31.997Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","dartlang","dependency-injection","flutter","library"],"created_at":"2025-06-15T07:39:18.671Z","updated_at":"2026-04-25T11:31:57.636Z","avatar_url":"https://github.com/stefalda.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InjectX\n\nA lightweight, easy-to-use service locator implementation for Dart applications. This package provides a simple dependency injection container that helps manage application dependencies with minimal setup.\n\n## Features\n\n- Simple registration and retrieval of dependencies\n- Singleton instance management\n- Automatic disposal of services\n- Type-safe dependency injection\n- Angular-style `inject` function\n- Zero external dependencies\n\n## Installation\n\nAdd this to your package's `pubspec.yaml` file:\n\n```yaml\ndependencies:\n  inject_x: ^0.0.5\n```\n\n## Usage\n\n### Basic Usage\n\n```dart\n// Register your dependencies\nInjectX.add\u003cUserService\u003e(UserService());\nInjectX.add\u003cAuthService\u003e(AuthService());\n\n// Retrieve instances\nfinal userService = InjectX.get\u003cUserService\u003e();\nfinal authService = InjectX.get\u003cAuthService\u003e();\n\n// Alternatively, use Angular-style injection\nfinal userService = inject\u003cUserService\u003e();\n```\n\n### Init Method\n\nIf your class defines an `init()` method, it will be called automatically **before the first injection**. This is useful when your service depends on other services that need to be resolved after construction — particularly in cases of circular dependencies.\n\n```dart\nclass UserService {\n  late AuthService authService;\n\n  void init() {\n    authService = inject\u003cAuthService\u003e();\n  }\n}\n```\n\nThe `init()` method is called **only once**, just before the first access via `get()` or `inject()`.\n\n### Async Init Method\n\nIf your class has an `async` `init()` method (returning a `Future`), you can use `injectAsync\u003cT\u003e()` or `InjectX.getAsync\u003cT\u003e()` to ensure initialization is awaited before use.\n\n```dart\nclass ConfigService {\n  String? config;\n\n  Future\u003cvoid\u003e init() async {\n    await Future.delayed(Duration(milliseconds: 100));\n    config = 'loaded';\n  }\n}\n\nvoid main() async {\n  InjectX.add(ConfigService());\n  final config = await injectAsync\u003cConfigService\u003e();\n}\n```\n\n### Automatic Disposal\n\nInjectX automatically calls a service’s `dispose()` method if it exists when the service is removed:\n\n```dart\nclass DatabaseService {\n  void dispose() {\n    // Cleanup resources\n  }\n}\n\n// Register the service\nInjectX.add\u003cDatabaseService\u003e(DatabaseService());\n\n// Later, remove it\nInjectX.remove\u003cDatabaseService\u003e(); // dispose() will be called automatically\n```\n\n## API Reference\n\n### Methods\n\n- `add\u003cT\u003e(T instance)`: Register a new dependency\n- `get\u003cT\u003e()`: Retrieve a registered dependency, calling `init()` if defined\n- `getAsync\u003cT\u003e()`: Retrieve a registered dependency, awaiting `init()` if it's async\n- `remove\u003cT\u003e()`: Remove a registered dependency and call `dispose()` if defined\n- `length`: The number of registered dependencies\n- `clear()`: Remove all dependencies and dispose each (if supported)\n\n### Helper Functions\n\n- `inject\u003cT\u003e()`: Angular-style helper to call `InjectX.get\u003cT\u003e()`\n- `injectAsync\u003cT\u003e()`: Angular-style helper for services with async `init()`\n\n## Error Handling\n\nThe package includes error handling for common scenarios:\n\n```dart\n// Attempting to retrieve a non-existent dependency\ntry {\n  final service = InjectX.get\u003cUnregisteredService\u003e();\n} catch (e) {\n  // Throws StateError: No instance registered for type UnregisteredService\n}\n```\n\n## Best Practices\n\n1. Register all dependencies early in your app lifecycle\n2. Use meaningful type parameters for clarity\n3. Implement `dispose()` for services that require cleanup\n4. Remove services when no longer needed (e.g., on module unload)\n\n## Example\n\n```dart\nclass UserService {\n  void dispose() {\n    // Cleanup\n  }\n}\n\nclass AuthService {\n  final UserService userService;\n\n  AuthService(this.userService);\n}\n\nvoid main() {\n  // Register services\n  InjectX.add\u003cUserService\u003e(UserService());\n\n  // Register a dependent service\n  InjectX.add\u003cAuthService\u003e(AuthService(inject\u003cUserService\u003e()));\n\n  // Use services\n  final userService = inject\u003cUserService\u003e();\n  final authService = inject\u003cAuthService\u003e();\n\n  // Cleanup\n  InjectX.remove\u003cAuthService\u003e();\n  InjectX.remove\u003cUserService\u003e();\n}\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a pull request.  \nFor major changes, open an issue first to discuss what you'd like to propose.\n\n## License\n\nThis project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefalda%2Finject_x","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefalda%2Finject_x","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefalda%2Finject_x/lists"}