Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/contributors-company/depend
dependencies is a Flutter package for managing dependencies and initializing them before the app starts. It provides easy access to dependencies and logs initialization times for performance tracking.
https://github.com/contributors-company/depend
dependency dependency-injection flutter
Last synced: about 2 months ago
JSON representation
dependencies is a Flutter package for managing dependencies and initializing them before the app starts. It provides easy access to dependencies and logs initialization times for performance tracking.
- Host: GitHub
- URL: https://github.com/contributors-company/depend
- Owner: contributors-company
- License: bsd-3-clause
- Created: 2024-09-08T23:46:13.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-28T01:38:02.000Z (2 months ago)
- Last Synced: 2024-10-28T05:24:12.606Z (2 months ago)
- Topics: dependency, dependency-injection, flutter
- Language: Dart
- Homepage: https://pub.dev/packages/depend
- Size: 85 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# depend
![Pub Version](https://img.shields.io/pub/v/depend)
![License](https://img.shields.io/github/license/AlexHCJP/depend)
![Issues](https://img.shields.io/github/issues/AlexHCJP/depend)
![Coverage](https://img.shields.io/codecov/c/github/contributors-company/depend)
![Stars](https://img.shields.io/github/stars/AlexHCJP/depend)
![Contributors](https://img.shields.io/github/contributors/AlexHCJP/depend)
![Watchers](https://img.shields.io/github/watchers/AlexHCJP/depend)
![Forks](https://img.shields.io/github/forks/AlexHCJP/depend)`depend` is a library for managing dependencies in Flutter applications. It provides a convenient way to initialize and access services or repositories via an `InheritedWidget`.
---
## Why it Rocks 🚀
- Initialize dependencies before launching the app
- Access dependencies from anywhere in the widget tree
- Log initialization times for each dependency
- Clean and extensible way to manage dependencies
- Easy to use and integrate with existing codebases---
- [dependencies](#dependencies)
- [Why it Rocks 🚀](#why-it-rocks-)
- [Installation](#installation)
- [Example Usage](#example-usage)
- [Example 1: Define Dependencies](#example-1-define-dependencies)
- [Step 2: Initialize Dependencies](#step-2-initialize-dependencies)
- [Step 3: Access Dependencies with `InheritedWidget`](#step-3-access-dependencies-with-inheritedwidget)
- [Example 2: Use Parent Dependencies](#example-2-use-parent-dependencies)
- [Step 1: Define Parent Dependencies](#step-1-define-parent-dependencies)
- [Logging and Debugging](#logging-and-debugging)## Installation
Add the package to your `pubspec.yaml`:
```yaml
dependencies:
depend: ^0.0.1
```Then run:
```bash
$ flutter pub get
```
---## Example Usage
### Example 1: Define Dependencies
#### Step 1: Extends `DependenciesLibrary`
Create a `DependenciesLibrary` that extends `DependenciesLibrary` and initializes your dependencies:
```dart
class RootLibrary extends DependenciesLibrary {
late final ApiService apiService;@override
Future init() async {
await log(() async => apiService = await ApiService().init());
}
}
```#### Step 2: Initialize Dependencies
Use `DependenciesInit` to initialize your dependencies before launching the app:
```dart
void main() {
runApp(
Dependencies(
library: RootLibrary(),
placeholder: const ColoredBox(
color: Colors.white,
child: Center(child: CircularProgressIndicator()),
),
child: const MyApp(),
),
);
}
```#### Step 3: Access Dependencies with `InheritedWidget`
Once initialized, dependencies can be accessed from anywhere in the widget tree using `Dependencies.of(context).authRepository`:
```dart
/// The repository for the example
final class AuthRepository {
final AuthDataSource dataSource;AuthRepository({required this.dataSource});
Future login() => dataSource.login();
}class MyApp extends StatelessWidget {
const MyApp({super.key});@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Dependencies(
library: ModuleLibrary(
parent: Dependencies.of(context),
),
child: BlocProvider(
create: (context) => DefaultBloc(
Dependencies.of(context).authRepository,
),
child: const MyHomePage(),
),
),
);
}
}class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});@override
State createState() => _MyHomePageState();
}class _MyHomePageState extends State {
void _login() {
context.read().add(DefaultEvent());
}@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
BlocBuilder(
builder: (context, state) {
return Text('Login: ${state.authorized}');
},
),
Builder(
builder: (context) {
return ElevatedButton(
onPressed: _login,
child: const Text('Login'),
);
},
)
],
),
),
),
);
}
}```
### Example 2: Use Parent Dependencies
#### Step 1: Define Parent Dependencies
```dart
class RootLibrary extends DependenciesLibrary {
late final ApiService apiService;@override
Future init() async {
apiService = await ApiService().init();
}
}class ModuleLibrary extends DependenciesLibrary {
late final AuthRepository authRepository;ModuleLibrary({required super.parent});
@override
Future init() async {
// initialize dependencies
authRepository = AuthRepository(
dataSource: AuthDataSource(
apiService: parent.apiService, // parent - RootLibrary
),
);
}
}```
## Logging and Debugging
During initialization, each dependency logs the time it took to initialize:
```dart
class ModuleLibrary extends DependenciesLibrary {
late final AuthRepository authRepository;ModuleLibrary({required super.parent});
@override
Future init() async {
await log(() async => authRepository = AuthRepository(
dataSource: AuthDataSource(
apiService: parent.apiService,
),
));
}
}
``````dart
💡 ApiService: initialized successfully for 10 ms
💡 AuthRepository: initialized successfully for 0 ms
```This is useful for tracking performance and initialization times.
## Codecov
![Codecov](https://codecov.io/gh/contributors-company/depend/graphs/sunburst.svg?token=DITZJ9E9OM)