Injecta
Injecta is a lightweight Flutter library that provides a simple and efficient service registry for managing dependencies in your Flutter applications. It can be used instead of InheritedWidget or Provider to access objects e.g. from your UI.
Installation
To use Injecta in your Flutter project, add the following dependency to your pubspec.yaml
file:
dependencies:
injecta: ^0.2.0
Then, run:
flutter pub get
Usage
- Create a
ServiceRegistry
Create your registry usingServiceRegistry
. Pass a list of functions that create instances of your services to the services parameter.
final services = ServiceRegistry(
services: [
() => CounterService(),
],
);
- Access Services in your Widgets
Use thecontext.get<T>()
method to access services within your widgets. The services will be lazily initialized and cached.
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
services.get<CounterService>().increment();
setState(() {});
},
child: Text('${services.get<CounterService>().counter}'),
);
}
}
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.
License
Injecta is distributed under the MIT License. See the LICENSE.md file for more information.
Libraries
- injecta
- A simple dependency injection library for Flutter applications.