{"id":336822,"date":"2022-08-09T21:00:17","date_gmt":"2022-08-09T21:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=336822"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=336822","title":{"rendered":"<span>CherryPick DI \u2014 di \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f dart\/flutter \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>CherryPick DI<\/strong><\/p>\n<p>DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u2013 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439.<\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p>1. \u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/p>\n<p>2. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/p>\n<p>3. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/p>\n<p>&#8212; 3.1. Scope<\/p>\n<p>&#8212; 3.2. Module<\/p>\n<p>&#8212; 3.3. Binding<\/p>\n<p>4. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<p>5. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<h3>1. \u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u0432\u043e\u0439 DI \u0434\u043b\u044f \u043f\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 Flutter SDK \u0431\u044b\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044b \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 2020 \u0433\u043e\u0434\u0430.<\/p>\n<p>\u0421\u043f\u043e\u0434\u0432\u0438\u0433\u043b\u043e \u043c\u0435\u043d\u044f \u043d\u0430 \u044d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0447\u0438\u043d: <\/p>\n<p>1. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b DI \u0432 pub.dev \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043d\u0430 scope (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043b\u043e\u0445\u043e \u0438\u0441\u043a\u0430\u043b)<\/p>\n<p>2. \u0423\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/p>\n<p>3. \u0416\u0435\u043b\u0430\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439  DI<\/p>\n<p>4. \u0418\u043c\u0435\u0442\u044c \u0432 \u0430\u0440\u0441\u0435\u043d\u0430\u043b\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 DI  (\u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API)<\/p>\n<h3>2. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/h3>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430:<\/p>\n<p>&#8212; \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c<\/p>\n<p>&#8212; \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u0430\u043a singleton<\/p>\n<p>&#8212; \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 (scopes)<\/p>\n<h3>3. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b DI<\/h3>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<p>&#8212; Scope<\/p>\n<p>&#8212; Module<\/p>\n<p>&#8212; Binding<\/p>\n<h4>3.1. Scope<\/h4>\n<p><strong>Scope<\/strong> &#8212; \u044d\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (scope,modules,instances).<\/p>\n<p>\u0427\u0435\u0440\u0435\u0437 scope \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <code>instance<\/code>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>resolve&lt;T>()<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<p><strong>Scope<\/strong> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. <\/p>\n<p><strong>Scope<\/strong> \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <strong>Scope<\/strong> \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 <strong>Scope<\/strong> \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u044d\u043a\u0440\u0430\u043d\u0430 \u0438\u043b\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u044d\u043a\u0440\u0430\u043d\u043e\u0432.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <strong>Scope<\/strong>, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u201c\u043e\u0442\u043a\u0440\u044b\u0442\u044c\u201d. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0434\u0438\u043d <strong>Scope<\/strong> \u043d\u0430 \u0432\u0441\u0451 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"dart\">final rootScope = CherryPick.openScope(named: 'appScope');<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044c <strong>Scope<\/strong> \u0441 \u0442\u0435\u043c \u0436\u0435 \u0441\u0430\u043c\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <strong>Scope<\/strong>.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 <strong>Scope<\/strong> \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u043c, \u0435\u0433\u043e (\u0438 \u0432\u0441\u0451 \u0434\u0435\u0440\u0435\u0432\u043e \u201c\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445\u201d <strong>Scope<\/strong>) \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>CherryPick.closeScope(name)<\/code><\/p>\n<h4>3.2. Module<\/h4>\n<p><strong>Module<\/strong> &#8212; \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u0430\u0432\u0438\u043b, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c CherryPick \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c <code>Scope<\/code>. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432 \u0441\u0432\u043e\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>void builder(Scope currentScope)<\/code>. \u041c\u043e\u0434\u0443\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 <code>Scope<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>scope.installModules(\u2026)<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e <code>Scope<\/code> \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c \u0432 \u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f\u0445.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"dart\">class AppModule extends Module {   @override   void builder(Scope currentScope) {     bind&lt;ApiClient>().toInstance(ApiClientMock());   } }<\/code><\/pre>\n<h4>3.3. Binding<\/h4>\n<p><strong>Binding<\/strong> &#8212; \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0442\u043e\u0440  \u0434\u043b\u044f  \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e instance, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e instance <code>toInstance()<\/code> \u0438 <code>toProvide()<\/code> \u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 <code>withName()<\/code> \u0438 <code>singleton()<\/code>.<\/p>\n<p><code>toInstance()<\/code> &#8212; \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440<\/p>\n<p><code>toProvide()<\/code> &#8212; \u00a0\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>provider<\/code> (\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430)<\/p>\n<p><code>withName()<\/code> &#8212; \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0434\u043b\u044f \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u044c instance \u0438\u0437  DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430<\/p>\n<p><code>singleton()<\/code> &#8212;  \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0444\u043b\u0430\u0433 \u0432 Binding, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0443, \u0447\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0434\u043d\u0430.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"dart\"> \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 toInstance()  bind&lt;String>().toInstance(\"hello world\");   \/\/ \u0438\u043b\u0438   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438  bind&lt;String>().toProvide(() => \"hello world\");   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c  bind&lt;String>().withName(\"my_string\").toInstance(\"hello world\");  \/\/ \u0438\u043b\u0438  bind&lt;String>().withName(\"my_string\").toProvide(() => \"hello world\");   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430, \u043a\u0430\u043a singleton  bind&lt;String>().toInstance(\"hello world\");  \/\/ \u0438\u043b\u0438  bind&lt;String>().toProvide(() => \"hello world\").singleton();<\/code><\/pre>\n<h3>4. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h3>\n<pre><code class=\"dart\">import 'dart:async'; import 'package:meta\/meta.dart'; import 'package:cherrypick\/cherrypick.dart';  class AppModule extends Module {   @override   void builder(Scope currentScope) {     bind&lt;ApiClient>().withName(\"apiClientMock\").toInstance(ApiClientMock());     bind&lt;ApiClient>().withName(\"apiClientImpl\").toInstance(ApiClientImpl());   } }  class FeatureModule extends Module {   bool isMock;    FeatureModule({required this.isMock});    @override   void builder(Scope currentScope) {     bind&lt;DataRepository>()         .withName(\"networkRepo\")         .toProvide(           () => NetworkDataRepository(             currentScope.resolve&lt;ApiClient>(               named: isMock ? \"apiClientMock\" : \"apiClientImpl\",             ),           ),         )         .singleton();     bind&lt;DataBloc>().toProvide(       () => DataBloc(         currentScope.resolve&lt;DataRepository>(named: \"networkRepo\"),       ),     );   } }  void main() async {   final scope = openRootScope().installModules([     AppModule(),   ]);    final subScope = scope       .openSubScope(\"featureScope\")       .installModules([FeatureModule(isMock: true)]);    final dataBloc = subScope.resolve&lt;DataBloc>();   dataBloc.data.listen((d) => print('Received data: $d'),       onError: (e) => print('Error: $e'), onDone: () => print('DONE'));    await dataBloc.fetchData(); }  class DataBloc {   final DataRepository _dataRepository;    Stream&lt;String> get data => _dataController.stream;   StreamController&lt;String> _dataController = new StreamController.broadcast();    DataBloc(this._dataRepository);    Future&lt;void> fetchData() async {     try {       _dataController.sink.add(await _dataRepository.getData());     } catch (e) {       _dataController.sink.addError(e);     }   }    void dispose() {     _dataController.close();   } }  abstract class DataRepository {   Future&lt;String> getData(); }  class NetworkDataRepository implements DataRepository {   final ApiClient _apiClient;   final _token = 'token';    NetworkDataRepository(this._apiClient);    @override   Future&lt;String> getData() async => await _apiClient.sendRequest(       url: 'www.google.com', token: _token, requestBody: {'type': 'data'}); }  abstract class ApiClient {   Future sendRequest({@required String url, String token, Map requestBody}); }  class ApiClientMock implements ApiClient {   @override   Future sendRequest(       {@required String? url, String? token, Map? requestBody}) async {     return 'Local Data';   } }  class ApiClientImpl implements ApiClient {   @override   Future sendRequest(       {@required String? url, String? token, Map? requestBody}) async {     return 'Network data';   } }<\/code><\/pre>\n<h3>5. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0442\u0440\u0435\u0445 \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>\u0417\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/pese-git.github.io\/cherrypick-site\/ru\/\" rel=\"noopener noreferrer nofollow\">\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p><a href=\"https:\/\/github.com\/pese-git\/cherrypick\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a>.<\/p>\n<p><a href=\"https:\/\/pub.dev\/packages\/cherrypick\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/681714\/\"> https:\/\/habr.com\/ru\/post\/681714\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>CherryPick DI<\/strong><\/p>\n<p>DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u2013 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439.<\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p>1. \u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/p>\n<p>2. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/p>\n<p>3. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/p>\n<p>&#8212; 3.1. Scope<\/p>\n<p>&#8212; 3.2. Module<\/p>\n<p>&#8212; 3.3. Binding<\/p>\n<p>4. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<p>5. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<h3>1. \u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u0432\u043e\u0439 DI \u0434\u043b\u044f \u043f\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 Flutter SDK \u0431\u044b\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044b \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 2020 \u0433\u043e\u0434\u0430.<\/p>\n<p>\u0421\u043f\u043e\u0434\u0432\u0438\u0433\u043b\u043e \u043c\u0435\u043d\u044f \u043d\u0430 \u044d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0447\u0438\u043d: <\/p>\n<p>1. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b DI \u0432 pub.dev \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043d\u0430 scope (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043b\u043e\u0445\u043e \u0438\u0441\u043a\u0430\u043b)<\/p>\n<p>2. \u0423\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/p>\n<p>3. \u0416\u0435\u043b\u0430\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439  DI<\/p>\n<p>4. \u0418\u043c\u0435\u0442\u044c \u0432 \u0430\u0440\u0441\u0435\u043d\u0430\u043b\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 DI  (\u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API)<\/p>\n<h3>2. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/h3>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430:<\/p>\n<p>&#8212; \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c<\/p>\n<p>&#8212; \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u0430\u043a singleton<\/p>\n<p>&#8212; \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 (scopes)<\/p>\n<h3>3. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b DI<\/h3>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432:<\/p>\n<p>&#8212; Scope<\/p>\n<p>&#8212; Module<\/p>\n<p>&#8212; Binding<\/p>\n<h4>3.1. Scope<\/h4>\n<p><strong>Scope<\/strong> &#8212; \u044d\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 (scope,modules,instances).<\/p>\n<p>\u0427\u0435\u0440\u0435\u0437 scope \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <code>instance<\/code>, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>resolve&lt;T>()<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<p><strong>Scope<\/strong> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. <\/p>\n<p><strong>Scope<\/strong> \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <strong>Scope<\/strong> \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 <strong>Scope<\/strong> \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u044d\u043a\u0440\u0430\u043d\u0430 \u0438\u043b\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u044d\u043a\u0440\u0430\u043d\u043e\u0432.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <strong>Scope<\/strong>, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u201c\u043e\u0442\u043a\u0440\u044b\u0442\u044c\u201d. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0434\u0438\u043d <strong>Scope<\/strong> \u043d\u0430 \u0432\u0441\u0451 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"dart\">final rootScope = CherryPick.openScope(named: 'appScope');<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044c <strong>Scope<\/strong> \u0441 \u0442\u0435\u043c \u0436\u0435 \u0441\u0430\u043c\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <strong>Scope<\/strong>.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 <strong>Scope<\/strong> \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u043c, \u0435\u0433\u043e (\u0438 \u0432\u0441\u0451 \u0434\u0435\u0440\u0435\u0432\u043e \u201c\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445\u201d <strong>Scope<\/strong>) \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043a\u0440\u044b\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>CherryPick.closeScope(name)<\/code><\/p>\n<h4>3.2. Module<\/h4>\n<p><strong>Module<\/strong> &#8212; \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u0430\u0432\u0438\u043b, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c CherryPick \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c <code>Scope<\/code>. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432 \u0441\u0432\u043e\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>void builder(Scope currentScope)<\/code>. \u041c\u043e\u0434\u0443\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 <code>Scope<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>scope.installModules(\u2026)<\/code>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e <code>Scope<\/code> \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c \u0432 \u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f\u0445.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"dart\">class AppModule extends Module {   @override   void builder(Scope currentScope) {     bind&lt;ApiClient>().toInstance(ApiClientMock());   } }<\/code><\/pre>\n<h4>3.3. Binding<\/h4>\n<p><strong>Binding<\/strong> &#8212; \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0442\u043e\u0440  \u0434\u043b\u044f  \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e instance, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e instance <code>toInstance()<\/code> \u0438 <code>toProvide()<\/code> \u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 <code>withName()<\/code> \u0438 <code>singleton()<\/code>.<\/p>\n<p><code>toInstance()<\/code> &#8212; \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440<\/p>\n<p><code>toProvide()<\/code> &#8212; \u00a0\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>provider<\/code> (\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430)<\/p>\n<p><code>withName()<\/code> &#8212; \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0434\u043b\u044f \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u044c instance \u0438\u0437  DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430<\/p>\n<p><code>singleton()<\/code> &#8212;  \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0444\u043b\u0430\u0433 \u0432 Binding, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0443, \u0447\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0434\u043d\u0430.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"dart\"> \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 toInstance()  bind&lt;String>().toInstance(\"hello world\");   \/\/ \u0438\u043b\u0438   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438  bind&lt;String>().toProvide(() => \"hello world\");   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c  bind&lt;String>().withName(\"my_string\").toInstance(\"hello world\");  \/\/ \u0438\u043b\u0438  bind&lt;String>().withName(\"my_string\").toProvide(() => \"hello world\");   \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430, \u043a\u0430\u043a singleton  bind&lt;String>().toInstance(\"hello world\");  \/\/ \u0438\u043b\u0438  bind&lt;String>().toProvide(() => \"hello world\").singleton();<\/code><\/pre>\n<h3>4. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h3>\n<pre><code class=\"dart\">import 'dart:async'; import 'package:meta\/meta.dart'; import 'package:cherrypick\/cherrypick.dart';  class AppModule extends Module {   @override   void builder(Scope currentScope) {     bind&lt;ApiClient>().withName(\"apiClientMock\").toInstance(ApiClientMock());     bind&lt;ApiClient>().withName(\"apiClientImpl\").toInstance(ApiClientImpl());   } }  class FeatureModule extends Module {   bool isMock;    FeatureModule({required this.isMock});    @override   void builder(Scope currentScope) {     bind&lt;DataRepository>()         .withName(\"networkRepo\")         .toProvide(           () => NetworkDataRepository(             currentScope.resolve&lt;ApiClient>(               named: isMock ? \"apiClientMock\" : \"apiClientImpl\",             ),           ),         )         .singleton();     bind&lt;DataBloc>().toProvide(       () => DataBloc(         currentScope.resolve&lt;DataRepository>(named: \"networkRepo\"),       ),     );   } }  void main() async {   final scope = openRootScope().installModules([     AppModule(),   ]);    final subScope = scope       .openSubScope(\"featureScope\")       .installModules([FeatureModule(isMock: true)]);    final dataBloc = subScope.resolve&lt;DataBloc>();   dataBloc.data.listen((d) => print('Received data: $d'),       onError: (e) => print('Error: $e'), onDone: () => print('DONE'));    await dataBloc.fetchData(); }  class DataBloc {   final DataRepository _dataRepository;    Stream&lt;String> get data => _dataController.stream;   StreamController&lt;String> _dataController = new StreamController.broadcast();    DataBloc(this._dataRepository);    Future&lt;void> fetchData() async {     try {       _dataController.sink.add(await _dataRepository.getData());     } catch (e) {       _dataController.sink.addError(e);     }   }    void dispose() {     _dataController.close();   } }  abstract class DataRepository {   Future&lt;String> getData(); }  class NetworkDataRepository implements DataRepository {   final ApiClient _apiClient;   final _token = 'token';    NetworkDataRepository(this._apiClient);    @override   Future&lt;String> getData() async => await _apiClient.sendRequest(       url: 'www.google.com', token: _token, requestBody: {'type': 'data'}); }  abstract class ApiClient {   Future sendRequest({@required String url, String token, Map requestBody}); }  class ApiClientMock implements ApiClient {   @override   Future sendRequest(       {@required String? url, String? token, Map? requestBody}) async {     return 'Local Data';   } }  class ApiClientImpl implements ApiClient {   @override   Future sendRequest(       {@required String? url, String? token, Map? requestBody}) async {     return 'Network data';   } }<\/code><\/pre>\n<h3>5. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0442\u0440\u0435\u0445 \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>\u0417\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/pese-git.github.io\/cherrypick-site\/ru\/\" rel=\"noopener noreferrer nofollow\">\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<p><a href=\"https:\/\/github.com\/pese-git\/cherrypick\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a>.<\/p>\n<p><a href=\"https:\/\/pub.dev\/packages\/cherrypick\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/681714\/\"> https:\/\/habr.com\/ru\/post\/681714\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-336822","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336822"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336822\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}