depend 5.0.0-dev
depend: ^5.0.0-dev copied to clipboard
depend simplifies dependency management in Flutter apps, providing easy initialization and access to services across the widget tree.
Hereβs the translated and updated README.md
for the depend
library:
Depend #
depend
is a library for dependency management in Flutter applications. It provides a convenient way to initialize and access services and repositories via InheritedWidget
.
Features π #
- Dependency Initialization: Prepare all dependencies before the app launches.
- Global Access: Access dependencies from anywhere in the widget tree.
- Parent Dependencies Support: Easily create nested or connected dependencies.
- Ease of Use: Integrate the library into existing code with minimal changes.
Table of Contents #
Installation #
Add the library to the pubspec.yaml
of your project:
dependencies:
depend: ^latest_version
Install the dependencies:
flutter pub get
Usage Examples #
Example 1: Simple Initialization #
Step 1: Define the Dependency
Create a class that extends DependencyContainer
and initialize your dependencies:
class RootDependency extends DependencyContainer {
final ApiService apiService;
RootDependency({required this.apiService});
void dispose() {
// apiService.dispose()
}
}
Step 2: Define the DependencyFactory
Create a class that extends DependencyContainer
and initialize your dependencies:
class RootDependencyFactory extends DependencyFactory<RootDependency> {
Future<RootDependency> create() async {
return RootDependency(
apiService: await ApiService.initialize(),
);
}
}
Step 3: Use DependencyScope
Wrap your app in a DependencyScope
to provide dependencies:
void main() {
runApp(
DependencyScope<RootDependency, RootDependencyFactory>(
dependency: RootDependencyFactory(),
placeholder: const Center(child: CircularProgressIndicator()),
builder: (BuildContext context) => const MyApp(),
),
);
}
Step 4: Access the Dependency in a Widget
You can now access the dependency using DependencyProvider
anywhere in the widget tree:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final apiService = DependencyProvider.of<RootDependency>(context).apiService;
return FutureBuilder(
future: apiService.getData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
return Text('Data: ${snapshot.data}');
},
);
}
}
Example 2: DependencyProvider
#
final RootDependency dep = await RootFactory().create();
DependencyProvider<RootDependency>(
dependency: dep,
builder: () => YourWidget();
// or
child: YourWidget()
)
class YourWidget extends StatelessWidget {
@override
Widget build(BuildContext) {
root = DependencyProvider.of<RootDependency>(context);
...
}
}
Example 3: DependencyScope
#
DependencyScope<RootDependency, RootFactory>(
factory: RootFactory(),
builder: (BuildContext context) => Text('Inject'),
placeholder: Text('Placeholder'),
errorBuilder: (Object? error) => Text('Error'),
),
Migration Guide #
link to migrate versions
Code Coverage #
This version reflects the latest changes and provides clear guidance for new users.