depend
depend फ्लटर ऐप में निर्भरता प्रबंधन को सरल बनाता है, जिससे विभिन्न विजेट वृक्ष में सेवाओं को आसानी से प्रारंभ और प्राप्त किया जा सकता है।
dependencies एक Flutter पैकेज है जिसका उपयोग डिपेंडेंसी के प्रबंधन और ऐप शुरू होने से पहले उन्हें प्रारंभ करने के लिए किया जाता है। इसमें डिपेंडेंसी तक आसान पहुंच प्रदान करता है और प्रदर्शन ट्रैकिंग के लिए प्रारंभीक समय को लॉग करता है।
{"sdk":"flutter"}{"sdk":"flutter"}^3.0.0^1.0.4यह अंग्रेज़ी मूल स्नैपशॉट है। नवीनतम सामग्री GitHub पर देखें।
Logo Frame
depend is a library for dependency management in Flutter applications. It provides a convenient way to initialize and access services and repositories via InheritedWidget.
Add the library to the pubspec.yaml of your project:
dependencies:
depend: ^latest_version
Install the dependencies:
flutter pub get
Create a class that extends DependencyContainer and initialize your dependencies:
class RootContainer extends DependencyContainer {
final ApiService apiService;
RootContainer({required this.apiService});
void dispose() {
apiService.dispose();
}
}
Create a class that extends DependencyContainer and initialize your dependencies:
class RootDependencyFactory extends DependencyFactory<RootContainer> {
Future<RootContainer> create() async {
return RootContainer(
apiService: await ApiService.initialize(),
);
}
// or
RootContainer create() {
return RootContainer(
apiService: ApiService.initialize(),
);
}
}
DependencyScopeWrap your app in a DependencyScope to provide dependencies:
void main() {
runApp(
DependencyScope<RootContainer, RootFactory>(
factory: RootFactory(),
placeholder: const Center(child: CircularProgressIndicator()),
builder: (BuildContext context) => const MyApp(),
),
);
}
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<RootContainer>(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}');
},
);
}
}
DependencyProviderfinal RootContainer dep = await RootFactory().create();
DependencyProvider<RootContainer>(
dependency: dep,
builder: () => YourWidget();
// or
child: YourWidget()
)
class YourWidget extends StatelessWidget {
@override
Widget build(BuildContext) {
root = DependencyProvider.of<RootContainer>(context);
...
}
}
DependencyScopeDependencyScope<RootContainer, RootFactory>(
factory: RootFactory(),
builder: (BuildContext context) => Text('Inject'),
placeholder: Text('Placeholder'),
errorBuilder: (Object? error) => Text('Error'),
),
The library provides LazyGet and LazyFutureGet classes for lazy initialization of services. Dependencies are created only when they are first accessed, which improves application startup time.
LazyGet for Synchronous Dependenciesclass RootContainer extends DependencyContainer {
// Service will be created only when accessed for the first time
final LazyGet<ApiService> apiService;
final LazyGet<DatabaseService> database;
}
class RootDependencyFactory extends DependencyFactory<RootContainer> {
RootContainer create() {
return RootContainer(
apiService: lazyGet(ApiService.initialize),
database: lazyGet(DatabaseService.initialize),
);
}
}
// Usage in widgets
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final container = DependencyProvider.of<RootContainer>(context);
// ApiService is created only at this moment
final api = container.apiService();
return Text('Service initialized: ${api}'); // Instance: ApiService
}
}
LazyFutureGet for Asynchronous Dependenciesclass RootContainer extends DependencyContainer {
final LazyFutureGet<DatabaseService> database;
final LazyFutureGet<AuthService> authService;
}
class RootDependencyFactory extends DependencyFactory<RootContainer> {
RootContainer create() {
return RootContainer(
apiService: lazyFutureGet(ApiService.initialize),
database: lazyFutureGet(DatabaseService.initialize),
);
}
}
// Usage in widgets
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final container = DependencyProvider.of<RootContainer>(context);
return FutureBuilder<DatabaseService>(
// Database is initialized only when this widget is built
future: container.database(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
return Text('Database ready: ${snapshot.data?.isConnected}');
},
);
}
}
Benefits of Lazy Initialization:
Codecov