सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
depend एक हल्का डिपेंडेंसी इंजेक्शन पैकेज है जो Flutter के लिए विभिन्न सर्विसेज के आरंभ और विज़ुअल ट्री में उपयोग को सरल बनाता है। यह डेवलपर्स को न्यूनतम बॉइलरप्लेट के साथ डिपेंडेंसी को रजिस्टर और प्राप्त करने की अनुमति देता है, जिससे स्पष्ट आर्किटेक्चर और आसान परीक्षण को बढ़ावा मिलता है। इस पैकेज को सभी प्रमुख प्लेटफॉर्म—Android, iOS, Web, macOS, Windows और Linux—के लिए समर्थित है और एक सरल pub add कमांड के माध्यम से मौजूदा Flutter प्रोजेक्ट में बिना किसी दिक्कत के एकीकृत किया जा सकता है। स्पष्ट एपीआई के साथ, depend कोड की रखरखाव को बढ़ावा देता है और ग्लोबल वेरिएबल या कॉन्टेक्स्ट-भारी पैटर्न पर निर्भरता को कम करता है।
उपयोग
कई स्क्रीन्स के माध्यम से साझा सेवाओं का प्रबंधन
इकाई और विजेट परीक्षण को सरल बनाना
जटिल प्रदाता सेटअप को बदलना
मॉड्यूलर ऐप संरचना सक्षम करें
निर्भरता सेटअप में बॉयलरप्लेट को कम करना
मुख्य विशेषताएँ
आसान सेवा पंजीकरण और प्राप्त करना
क्रॉस-प्लेटफ़ॉर्म समर्थन
न्यूनतम बॉयलरप्लेट
निर्मित जीवनचक्र प्रबंधन
सिंगलेटन और ट्रांजिएंट इंस्टेंस दोनों का समर्थन करता है
इनके लिए उपयुक्त
छोटे से मध्यम आकार के Flutter एप्लिकेशन
सरल डीआई समाधान खोज रहे टीमें
स्पष्ट चिंता के विभाजन की आवश्यकता वाले प्रोजेक्ट
भारी अवस्था प्रबंधन पुस्तकालयों से बचने वाले विकासकर्ता
बहुप्लेटफॉर्म लक्षित ऐप्स
विचार योग्य बातें
अत्यधिक जटिल निर्भरता ग्राफ के लिए उपयुक्त नहीं हो सकता
पूर्ण DI फ्रेमवर्क की तुलना में सीमित उन्नत विशेषताएं
सरल निर्भरता की आवश्यकताओं के लिए सबसे अच्छा
व्यापक रनटाइम कॉन्फ़िगरेशन वाले एप्लिकेशन के लिए आदर्श नहीं है
बड़े पैमाने पर एप्लिकेशन के लिए सावधानीपूर्वक योजना की आवश्यकता होती है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
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.
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.
Using LazyGet for Synchronous Dependencies
classRootContainerextendsDependencyContainer{
// Service will be created only when accessed for the first timefinalLazyGet<ApiService> apiService;
finalLazyGet<DatabaseService> database;
}
classRootDependencyFactoryextendsDependencyFactory<RootContainer> {
RootContainercreate() {
returnRootContainer(
apiService: lazyGet(ApiService.initialize),
database: lazyGet(DatabaseService.initialize),
);
}
}
// Usage in widgetsclassMyWidgetextendsStatelessWidget{
@overrideWidgetbuild(BuildContext context) {
final container = DependencyProvider.of<RootContainer>(context);
// ApiService is created only at this momentfinal api = container.apiService();
returnText('Service initialized: ${api}'); // Instance: ApiService
}
}
Using LazyFutureGet for Asynchronous Dependencies
classRootContainerextendsDependencyContainer{
finalLazyFutureGet<DatabaseService> database;
finalLazyFutureGet<AuthService> authService;
}
classRootDependencyFactoryextendsDependencyFactory<RootContainer> {
RootContainercreate() {
returnRootContainer(
apiService: lazyFutureGet(ApiService.initialize),
database: lazyFutureGet(DatabaseService.initialize),
);
}
}
// Usage in widgetsclassMyWidgetextendsStatelessWidget{
@overrideWidgetbuild(BuildContext context) {
final container = DependencyProvider.of<RootContainer>(context);
returnFutureBuilder<DatabaseService>(
// Database is initialized only when this widget is built
future: container.database(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
returnCircularProgressIndicator();
}
returnText('Database ready: ${snapshot.data?.isConnected}');
},
);
}
}
Benefits of Lazy Initialization:
Faster App Startup: Dependencies are created only when needed