{"id":15638150,"url":"https://github.com/jonsamwell/flutter_simple_dependency_injection","last_synced_at":"2025-04-14T16:17:08.745Z","repository":{"id":52894161,"uuid":"138949709","full_name":"jonsamwell/flutter_simple_dependency_injection","owner":"jonsamwell","description":"A super simple dependency injection implementation for flutter that behaviours like any normal IOC container and does not rely on mirrors","archived":false,"fork":false,"pushed_at":"2021-04-14T22:15:50.000Z","size":40,"stargazers_count":94,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T16:16:57.702Z","etag":null,"topics":["dart","dependency-injection","flutter","flutter-plugin"],"latest_commit_sha":null,"homepage":null,"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/jonsamwell.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}},"created_at":"2018-06-28T01:12:15.000Z","updated_at":"2024-08-29T11:43:49.000Z","dependencies_parsed_at":"2022-08-23T08:10:43.657Z","dependency_job_id":null,"html_url":"https://github.com/jonsamwell/flutter_simple_dependency_injection","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsamwell%2Fflutter_simple_dependency_injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsamwell%2Fflutter_simple_dependency_injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsamwell%2Fflutter_simple_dependency_injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonsamwell%2Fflutter_simple_dependency_injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonsamwell","download_url":"https://codeload.github.com/jonsamwell/flutter_simple_dependency_injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248914117,"owners_count":21182359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","dependency-injection","flutter","flutter-plugin"],"created_at":"2024-10-03T11:19:38.443Z","updated_at":"2025-04-14T16:17:08.715Z","avatar_url":"https://github.com/jonsamwell.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# flutter_simple_dependency_injection\n\nA simple dependency injection plugin for [Flutter](https://flutter.io) and Dart.\n\nThis implementation *does not* rely on the dart reflection apis (mirrors) and favours a simple factory based approach.\nThis increases the performance and simplicity of this implementation.\n\n* Support for multiple injectors (useful for unit testing or code running in isolates)\n* Support for types and named types\n* Support for singletons\n* Support simple values (useful for configuration parameters like api keys or urls)\n\nAny help is appreciated! Comment, suggestions, issues, PR's!\n\n## Getting Started\n\nIn your flutter or dart project add the dependency:\n\n``` yml\ndependencies:\n  ...\n  flutter_simple_dependency_injection: any\n```\n\nFor help getting started with Flutter, view the online\n[documentation](https://flutter.io/).\n\n## Usage example\n\nImport `flutter_simple_dependency_injection`\n\n``` dart\nimport 'package:flutter_simple_dependency_injection/injector.dart';\n```\n\n### Injector Configuration\n\nAs this injector relies on factories rather than reflection (as mirrors in not available in Flutter)\neach mapped type needs to provide a factory function.  In most cases this can just be a simple \nnew object returned function. In slightly more advanced scenarios where the type to be created relies\non other types an injector instances is passed into the factory function to allow the type of be created\nto get other types it depends on (see below for examples).\n\n``` dart\nimport 'package:flutter_simple_dependency_injection/injector.dart';\n\nvoid main() {\n  // it is best to place all injector initialisation work into one or more modules\n  // so it can act more like a dependency injector than a service locator.\n  // The Injector implements a singleton pattern. You can get a singleton injector instance\n  // just by calling Injector().\n  final injector = ModuleContainer().initialise(Injector());\n\n  // NOTE: it is best to architect your code so that you never need to\n  // interact with the injector itself.  Make this framework act like a dependency injector\n  // by having dependencies injected into objects in their constructors.  That way you avoid\n  // any tight coupling with the injector itself.\n\n  // Basic usage, however this kind of tight couple and direct interaction with the injector\n  // should be limited.  Instead prefer dependencies injection.\n\n  // simple dependency retrieval and method call\n  injector.get\u003cSomeService\u003e().doSomething();\n\n  // get an instance of each of the same mapped types\n  final instances = injector.getAll\u003cSomeType\u003e();\n  print(instances.length); // prints '3'\n\n  // passing in additional arguments when getting an instance\n  final instance =\n      injector.get\u003cSomeOtherType\u003e(additionalParameters: {\"id\": \"some-id\"});\n  print(instance.id); // prints 'some-id'\n}\n\nclass ModuleContainer {\n  Injector initialise(Injector injector) {\n    injector.map\u003cLogger\u003e((i) =\u003e Logger(), isSingleton: true);\n    injector.map\u003cString\u003e((i) =\u003e \"https://api.com/\", key: \"apiUrl\");\n    injector.map\u003cSomeService\u003e(\n        (i) =\u003e SomeService(i.get\u003cLogger\u003e(), i.get\u003cString\u003e(key: \"apiUrl\")));\n\n    // note that you can configure mapping in a fluent programming style too\n    injector.map\u003cSomeType\u003e((injector) =\u003e SomeType(\"0\"))\n            ..map\u003cSomeType\u003e((injector) =\u003e SomeType(\"1\"), key: \"One\")\n            ..map\u003cSomeType\u003e((injector) =\u003e SomeType(\"2\"), key: \"Two\");\n\n    injector.mapWithParams\u003cSomeOtherType\u003e((i, p) =\u003e SomeOtherType(p[\"id\"]));\n\n    return injector;\n  }\n}\n\nclass Logger {\n  void log(String message) =\u003e print(message);\n}\n\nclass SomeService {\n  final Logger _logger;\n  final String _apiUrl;\n\n  SomeService(this._logger, this._apiUrl);\n\n  void doSomething() {\n    _logger.log(\"Doing something with the api at '$_apiUrl'\");\n  }\n}\n\nclass SomeType {\n  final String id;\n  SomeType(this.id);\n}\n\nclass SomeOtherType {\n  final String id;\n  SomeOtherType(this.id);\n}\n\n```\n\n### Remove mappings\n\nYou can remove a mapped a factory/instance at any time:\n\n``` \n  injector.removeMapping\u003cSomeType\u003e();\n\n  injector.removeMapping\u003cSomeType\u003e(key: 'some_key');\n\n  injector.removeAllMappings\u003cSomeType\u003e();\n```\n\n### Multiple Injectors\n\nThe Injector class has a factory constructor that by default returns the default singleton instance of the injector. In most cases this will be enough.\nHowever, you can pass a name into this factory constructor to return another isolated injector that is independent from the default injector. Passing in a new \ninjector name will create the injector if it has not be retrieved before.\n\n``` dart\n  final defaultInjector = Injector();\n  final isolatedInjector = Injector(\"Isolated\");\n```\n\nTo destroy an injector instance call its `dispose` method. The particular instance of the injector with all mappings will be removed.\n\n``` dart\n  Injector().dispose();\n  Injector(\"Isolated\").dispose();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonsamwell%2Fflutter_simple_dependency_injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonsamwell%2Fflutter_simple_dependency_injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonsamwell%2Fflutter_simple_dependency_injection/lists"}