{"id":357739,"date":"2024-05-20T23:57:01","date_gmt":"2024-05-20T23:57:01","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=357739"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=357739","title":{"rendered":"<span>Event Bus: \u043f\u0438\u0448\u0435\u043c \u0448\u0438\u043d\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u043e Flutter-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/998\/c8b\/8a2\/998c8b8a2e9e0798a2596fe7a8df0edf.jpeg\" width=\"1600\" height=\"901\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/998\/c8b\/8a2\/998c8b8a2e9e0798a2596fe7a8df0edf.jpeg\" data-blurred=\"true\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u042e\u0440\u0438\u0439 \u041f\u0435\u0442\u0440\u043e\u0432, \u044f Flutter Team Lead <\/em><a href=\"https:\/\/friflex.com\/\"><em><u>\u0432 Friflex<\/u><\/em><\/a><em>. \u041a\u0430\u043a \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u0433\u0438, \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u0432\u043e Flutter \u0438\u0437 \u043c\u0438\u0440\u0430 Android. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f Android \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u044f\u043d\u0435\u043c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0432 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0432\u0430\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u0447\u0443\u0434\u0435\u0441\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 Event Bus. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u044d\u0442\u043e\u0442 \u0442\u0435\u0440\u043c\u0438\u043d \u0434\u043e\u0441\u043b\u043e\u0432\u043d\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u00ab\u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u00bb.<\/em><\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e, \u043c\u0435\u0441\u0442\u043e\u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d \u043f\u0440\u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0435 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u0420\u0430\u0437\u043d\u044b\u0435 \u043a\u0435\u0439\u0441\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u043b \u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0442\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0441\u0435\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 \u0433\u043e\u0440\u043e\u0434\u0443. \u0418\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u0448\u0435\u043b \u0438\u0437 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430, \u0442\u043e \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434 \u043e \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0434\u0440\u0443\u0433 \u043d\u0430 \u0434\u0440\u0443\u0433\u0430. \u041a\u0430\u043a \u0440\u0430\u0437 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u0430\u043a \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<h2>\u041f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Event Bus<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0440 (Publisher), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u043a\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043f\u043e\u0442\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445. \u0418 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 (Subscribers) \u2014 \u043e\u043d\u0438 \u0436\u0434\u0443\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0435, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043d\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d\u044b \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u00ab\u0441\u043b\u0430\u0431\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445\u00bb \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/daa\/c9d\/72b\/daac9d72b4e6f67430ff65e843cdb51b.jpg\" width=\"2045\" height=\"1152\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/daa\/c9d\/72b\/daac9d72b4e6f67430ff65e843cdb51b.jpg\" data-blurred=\"true\"\/><\/figure>\n<h2>\u041f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0431\u0435\u0437 \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u0443\u0442\u044c \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 flutter_bloc.<\/p>\n<p>\u041c\u044b \u0438\u043c\u0435\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a (\u0441\u043a\u0435\u043b\u0435\u0442\u043e\u043d-\u043f\u0440\u043e\u0435\u043a\u0442 \u0432\u043e Flutter), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0447\u0435\u0442\u043d\u043e\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/0de\/c3f\/bbc\/0dec3fbbcacdf15985ac17aad15f152e.jpeg\" width=\"1600\" height=\"901\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0de\/c3f\/bbc\/0dec3fbbcacdf15985ac17aad15f152e.jpeg\" data-blurred=\"true\"\/><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043a\u0443\u0431\u0438\u0442\u0430. \u041f\u0435\u0440\u0432\u044b\u0439 \u043a\u0443\u0431\u0438\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u0427\u0442\u043e\u0431\u044b \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 CounterCubit \u0432 ListenCubit. \u0410 \u0432 ListenCubit \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443 \u0432 \u0432\u0438\u0434\u0435 StreamSubscription \u043d\u0430 CounterCubit. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f bloc\/cubit, \u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u0438\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u0436\u0435\u0441\u0442\u043a\u043e \u0431\u043b\u043e\u043a\u0438. \u041f\u0440\u043e \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 flutter_bloc. <a href=\"https:\/\/bloclibrary.dev\/#\/architecture?id=bloc-to-bloc-communication\">https:\/\/bloclibrary.dev\/#\/architecture?id=bloc-to-bloc-communication<\/a>. \u041d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0442\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0445\u043e\u0440\u043e\u0448\u043e \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0448\u0438\u043d\u044b.<\/p>\n<details class=\"spoiler\">\n<summary>counter_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:flutter_bloc\/flutter_bloc.dart';  class CounterCubit extends Cubit&lt;int> {   CounterCubit() : super(0);    void increment() => emit(state + 1); } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>listen_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">class ListenCubit extends Cubit&lt;String> {   ListenCubit(this.counterCubit) : super('Start') {     subscription = counterCubit.stream.listen((event) {       if (event.isEven) {       emit(\"\u0427\u0438\u0441\u043b\u043e \u0447\u0435\u0442\u043d\u043e\u0435 $event\");       }     });   }    final CounterCubit counterCubit;   late final StreamSubscription subscription;    @override   Future&lt;void> close() {     subscription.cancel();     return super.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>main.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">void main() {   runApp(MaterialApp(     theme: ThemeData(     colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),     useMaterial3: true,     ),     home: const App())); }   class App extends StatelessWidget {   const App({super.key});    @override   Widget build(BuildContext context) {     return MultiBlocProvider(       providers: [         BlocProvider(           create: (context) => CounterCubit(),         ),         BlocProvider(           create: (context) => ListenCubit(context.read&lt;CounterCubit>()),         ),       ],     child: _CounterScreenView(),     );   } }   class _CounterScreenView extends StatelessWidget {   @override   Widget build(BuildContext context) {     return Scaffold(       floatingActionButton: FloatingActionButton(         onPressed: () => context.read&lt;CounterCubit>().increment(),         child: const Icon(Icons.add),       ),       appBar: AppBar(         backgroundColor: Theme.of(context).colorScheme.inversePrimary,       ),       body: Padding(       padding: const EdgeInsets.all(16),       child: Stack(       children: [         const SizedBox(height: 30),         BlocBuilder&lt;ListenCubit, String>(             builder: (context, state) {               return Text(                   state.toString(),                   style: Theme.of(context).textTheme.headlineMedium,               );             },         ),       Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: &lt;Widget>[               const SizedBox(height: 30),               BlocBuilder&lt;CounterCubit, int>(                 builder: (context, state) {                   return Text(                     state.toString(),                     style: Theme.of(context).textTheme.headlineMedium,                   );})]))])));   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 bloc to bloc:<\/p>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/summary>\n<div class=\"spoiler__content\">\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/652e805d72e08276d7b30031\" data-style=\"\" id=\"652e805d72e08276d7b30031\" width=\"\"><\/div>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u041f\u0438\u0448\u0435\u043c \u044d\u0442\u0443 \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u043d\u043e \u0443\u0436\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0421\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043e\u0431\u0449\u0443\u044e \u0441\u0445\u0435\u043c\u0443, \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0430\u043c\u0443 \u0448\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430\u043c. \u0414\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 (Stream), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0445\u043e\u0434\u044f\u0442 \u0432 SDK Dart.<\/p>\n<details class=\"spoiler\">\n<summary>app_event.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'dart:async'; import 'event.dart';   final class EventBus {   final StreamController _streamController = StreamController.broadcast();    \/\/\/ \u041c\u0435\u0442\u043e\u0434, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u0438\u043c \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f   Stream&lt;T> on&lt;T>() =>     _streamController.stream.where((event) => event is T).cast&lt;T>();    \/\/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u0448\u0438\u043d\u0443   void addEvent(Event event) {     _streamController.add(event);   }    \/\/\/ \u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c, \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d.   \/\/\/ \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442   \/\/\/ \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0422\u0430\u043a \u0436\u0435, \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435    \/\/\/ \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432.   void dispose() {     _streamController.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Even c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u044f Event&lt;T>.<\/p>\n<details class=\"spoiler\">\n<summary>event.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:equatable\/equatable.dart';   \/\/\/ \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 abstract class Event&lt;T> extends Equatable {   const Event(this.data);    final T data;     @override   List&lt;Object?> get props => [data]; }   \/\/\/ \u0421\u043e\u0431\u044b\u0442\u0438\u0435 - \u0435\u0441\u043b\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f class CounterIsEven&lt;int> extends Event {   const CounterIsEven(data) : super('\u0427\u0438\u0441\u043b\u043e \u0447\u0435\u0442\u043d\u043e\u0435 $data'); } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c ListenCubit:<\/p>\n<details class=\"spoiler\">\n<summary>listen_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'dart:async'; import 'package:flutter_bloc\/flutter_bloc.dart'; import 'package:mitap_event_bus\/event_bus\/event.dart'; import 'package:mitap_event_bus\/event_bus\/event_bus.dart';  class ListenCubit extends Cubit&lt;String> {   ListenCubit(this.eventBus) : super('Start') {     subscription = eventBus.on&lt;CounterIsEven>().listen((event) {     emit(event.data);   }); }    final EventBus eventBus;   late final StreamSubscription subscription;      @override   Future&lt;void> close() {     subscription.cancel();     return super.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0427\u0442\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u043e\u0441\u044c? \u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0435 \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 CounterCubit \u2014 \u043c\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 EventBus. <strong>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u043c.<\/strong> \u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u0430\u043a Singleton \u0432 DI \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0436\u0434\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0435 CounterEvent (\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u2014 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0447\u0435\u0442\u043d\u043e\u0435). \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u043f\u043e \u0448\u0438\u043d\u0435, \u043d\u0430\u0441 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442.<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c main.dart:<\/p>\n<details class=\"spoiler\">\n<summary>main.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:flutter\/material.dart'; import 'package:flutter_bloc\/flutter_bloc.dart'; import 'package:mitap_event_bus\/cubits\/counter_cubit\/counter_cubit.dart'; import 'package:mitap_event_bus\/cubits\/listen_cubit\/listen_cubit.dart'; import 'package:mitap_event_bus\/event_bus\/event_bus.dart';  import 'event_bus\/event.dart';  \/\/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439 EventBus eventBus = EventBus();  void main() {   runApp(MaterialApp(     theme: ThemeData(     colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),     useMaterial3: true,     ),     home: const App())); }   class App extends StatelessWidget { const App({super.key});    @override   Widget build(BuildContext context) {     return MultiBlocProvider(       providers: [         BlocProvider(       create: (context) => CounterCubit()),         BlocProvider(       create: (context) => ListenCubit(eventBus)),       ],       child: _CounterScreenView());   } }   class _CounterScreenView extends StatelessWidget {   @override   Widget build(BuildContext context) {     return Scaffold(       floatingActionButton: FloatingActionButton(       onPressed: () {         context.read&lt;CounterCubit>().increment();         \/\/\/ \u041f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0438\u0306 \u0441\u043c\u0435\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u0448\u0438\u043d\u0443         \/\/\/ \u0435\u0441\u043b\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0447\u0435\u0442\u043d\u044b\u0438\u0306         final counter = context.read&lt;CounterCubit>().state;         if (counter.isEven) {           eventBus.addEvent(CounterIsEven(counter));         }},       child: const Icon(Icons.add)),       appBar: AppBar(         backgroundColor: Theme.of(context).colorScheme.inversePrimary,         ),         body: Padding(         padding: const EdgeInsets.all(16),         child: Stack(         children: [           const SizedBox(height: 30),           BlocBuilder&lt;ListenCubit, String>(             builder: (context, state) {               return Text(                 state.toString(),                 style: Theme.of(context).textTheme.headlineMedium,               );             },           ),           Center(             child: Column(             mainAxisAlignment: MainAxisAlignment.center,             children: &lt;Widget>[               const SizedBox(height: 30),               BlocBuilder&lt;CounterCubit, int>(                 builder: (context, state) {                   return Text(                   state.toString(),                   style: Theme.of(context).textTheme.headlineMedium,                   );                 },             )]))])));   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0441\u0442\u0440\u043e\u043a\u0435 49 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 \u043e\u043d\u043e \u0447\u0435\u0442\u043d\u043e\u0435, \u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0448\u0438\u043d\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u044f CounterEvent.<\/p>\n<h2>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/h2>\n<ul>\n<li>\n<p>\u0423 \u0432\u0430\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d Publisher (\u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0439) \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u042d\u0442\u043e \u0443\u0431\u0435\u0440\u0435\u0436\u0435\u0442 \u0432\u0430\u0441 \u043e\u0442 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0431\u043e\u043b\u0438, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0438 \u043e\u0442 \u0447\u0435\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0438 \u043a\u0442\u043e \u043d\u0430 \u0447\u0442\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d.<\/p>\n<\/li>\n<li>\n<p>\u0428\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0437\u0430\u0432\u044f\u0437\u0430\u043d\u0430 \u043d\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 flutter_bloc \u0438\u043b\u0438 \u043d\u0430 \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u0442\u0440\u0438\u043c. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0438 \u0432\u0435\u0437\u0434\u0435.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435: \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0438 \u043a\u0442\u043e \u043d\u0430 \u043d\u0438\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d.<\/p>\n<\/li>\n<li>\n<p>\u0428\u0438\u043d\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0442\u0430\u043a \u043a\u0430\u043a \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0437\u0430\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043b\u0430 \u0441\u0435\u0431\u044f \u043d\u0430 \u043d\u0438\u0445 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/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\/articles\/768060\/\"> https:\/\/habr.com\/ru\/articles\/768060\/<\/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<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u042e\u0440\u0438\u0439 \u041f\u0435\u0442\u0440\u043e\u0432, \u044f Flutter Team Lead <\/em><a href=\"https:\/\/friflex.com\/\"><em><u>\u0432 Friflex<\/u><\/em><\/a><em>. \u041a\u0430\u043a \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u0433\u0438, \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u0432\u043e Flutter \u0438\u0437 \u043c\u0438\u0440\u0430 Android. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f Android \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u044f\u043d\u0435\u043c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0432 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0432\u0430\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u0447\u0443\u0434\u0435\u0441\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 Event Bus. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u044d\u0442\u043e\u0442 \u0442\u0435\u0440\u043c\u0438\u043d \u0434\u043e\u0441\u043b\u043e\u0432\u043d\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u00ab\u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u00bb.<\/em><\/p>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e, \u043c\u0435\u0441\u0442\u043e\u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0433\u0430\u0437\u0438\u043d \u043f\u0440\u0438 \u043f\u043e\u043a\u0443\u043f\u043a\u0435 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u0420\u0430\u0437\u043d\u044b\u0435 \u043a\u0435\u0439\u0441\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u043b \u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0442\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u0434\u0435\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0441\u0435\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u043e\u0432 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 \u0433\u043e\u0440\u043e\u0434\u0443. \u0418\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u0448\u0435\u043b \u0438\u0437 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430, \u0442\u043e \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434 \u043e \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0434\u0440\u0443\u0433 \u043d\u0430 \u0434\u0440\u0443\u0433\u0430. \u041a\u0430\u043a \u0440\u0430\u0437 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u0430\u043a \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<h2>\u041f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Event Bus<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0440 (Publisher), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u043a\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u043f\u043e\u0442\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445. \u0418 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 (Subscribers) \u2014 \u043e\u043d\u0438 \u0436\u0434\u0443\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0435, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043d\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d\u044b \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u00ab\u0441\u043b\u0430\u0431\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445\u00bb \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u041f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0431\u0435\u0437 \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u0443\u0442\u044c \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 flutter_bloc.<\/p>\n<p>\u041c\u044b \u0438\u043c\u0435\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a (\u0441\u043a\u0435\u043b\u0435\u0442\u043e\u043d-\u043f\u0440\u043e\u0435\u043a\u0442 \u0432\u043e Flutter), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u043d\u0430\u0436\u0430\u0442\u0438\u0438 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0447\u0435\u0442\u043d\u043e\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. <\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043a\u0443\u0431\u0438\u0442\u0430. \u041f\u0435\u0440\u0432\u044b\u0439 \u043a\u0443\u0431\u0438\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u0427\u0442\u043e\u0431\u044b \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 CounterCubit \u0432 ListenCubit. \u0410 \u0432 ListenCubit \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443 \u0432 \u0432\u0438\u0434\u0435 StreamSubscription \u043d\u0430 CounterCubit. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f bloc\/cubit, \u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u0438\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u0436\u0435\u0441\u0442\u043a\u043e \u0431\u043b\u043e\u043a\u0438. \u041f\u0440\u043e \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 flutter_bloc. <a href=\"https:\/\/bloclibrary.dev\/#\/architecture?id=bloc-to-bloc-communication\">https:\/\/bloclibrary.dev\/#\/architecture?id=bloc-to-bloc-communication<\/a>. \u041d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0442\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0445\u043e\u0440\u043e\u0448\u043e \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0448\u0438\u043d\u044b.<\/p>\n<details class=\"spoiler\">\n<summary>counter_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:flutter_bloc\/flutter_bloc.dart';  class CounterCubit extends Cubit&lt;int> {   CounterCubit() : super(0);    void increment() => emit(state + 1); } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>listen_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">class ListenCubit extends Cubit&lt;String> {   ListenCubit(this.counterCubit) : super('Start') {     subscription = counterCubit.stream.listen((event) {       if (event.isEven) {       emit(\"\u0427\u0438\u0441\u043b\u043e \u0447\u0435\u0442\u043d\u043e\u0435 $event\");       }     });   }    final CounterCubit counterCubit;   late final StreamSubscription subscription;    @override   Future&lt;void> close() {     subscription.cancel();     return super.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>main.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">void main() {   runApp(MaterialApp(     theme: ThemeData(     colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),     useMaterial3: true,     ),     home: const App())); }   class App extends StatelessWidget {   const App({super.key});    @override   Widget build(BuildContext context) {     return MultiBlocProvider(       providers: [         BlocProvider(           create: (context) => CounterCubit(),         ),         BlocProvider(           create: (context) => ListenCubit(context.read&lt;CounterCubit>()),         ),       ],     child: _CounterScreenView(),     );   } }   class _CounterScreenView extends StatelessWidget {   @override   Widget build(BuildContext context) {     return Scaffold(       floatingActionButton: FloatingActionButton(         onPressed: () => context.read&lt;CounterCubit>().increment(),         child: const Icon(Icons.add),       ),       appBar: AppBar(         backgroundColor: Theme.of(context).colorScheme.inversePrimary,       ),       body: Padding(       padding: const EdgeInsets.all(16),       child: Stack(       children: [         const SizedBox(height: 30),         BlocBuilder&lt;ListenCubit, String>(             builder: (context, state) {               return Text(                   state.toString(),                   style: Theme.of(context).textTheme.headlineMedium,               );             },         ),       Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: &lt;Widget>[               const SizedBox(height: 30),               BlocBuilder&lt;CounterCubit, int>(                 builder: (context, state) {                   return Text(                     state.toString(),                     style: Theme.of(context).textTheme.headlineMedium,                   );})]))])));   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0447\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 bloc to bloc:<\/p>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/summary>\n<div class=\"spoiler__content\">\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/652e805d72e08276d7b30031\" data-style=\"\" id=\"652e805d72e08276d7b30031\" width=\"\"><\/div>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u041f\u0438\u0448\u0435\u043c \u044d\u0442\u0443 \u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u043d\u043e \u0443\u0436\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0421\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043e\u0431\u0449\u0443\u044e \u0441\u0445\u0435\u043c\u0443, \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0430\u043c\u0443 \u0448\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430\u043c. \u0414\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 (Stream), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0445\u043e\u0434\u044f\u0442 \u0432 SDK Dart.<\/p>\n<details class=\"spoiler\">\n<summary>app_event.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'dart:async'; import 'event.dart';   final class EventBus {   final StreamController _streamController = StreamController.broadcast();    \/\/\/ \u041c\u0435\u0442\u043e\u0434, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u0438\u043c \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f   Stream&lt;T> on&lt;T>() =>     _streamController.stream.where((event) => event is T).cast&lt;T>();    \/\/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u0448\u0438\u043d\u0443   void addEvent(Event event) {     _streamController.add(event);   }    \/\/\/ \u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c, \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d.   \/\/\/ \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442   \/\/\/ \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0422\u0430\u043a \u0436\u0435, \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435    \/\/\/ \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432.   void dispose() {     _streamController.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Even c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u044f Event&lt;T>.<\/p>\n<details class=\"spoiler\">\n<summary>event.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:equatable\/equatable.dart';   \/\/\/ \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 abstract class Event&lt;T> extends Equatable {   const Event(this.data);    final T data;     @override   List&lt;Object?> get props => [data]; }   \/\/\/ \u0421\u043e\u0431\u044b\u0442\u0438\u0435 - \u0435\u0441\u043b\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f class CounterIsEven&lt;int> extends Event {   const CounterIsEven(data) : super('\u0427\u0438\u0441\u043b\u043e \u0447\u0435\u0442\u043d\u043e\u0435 $data'); } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c ListenCubit:<\/p>\n<details class=\"spoiler\">\n<summary>listen_cubit.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'dart:async'; import 'package:flutter_bloc\/flutter_bloc.dart'; import 'package:mitap_event_bus\/event_bus\/event.dart'; import 'package:mitap_event_bus\/event_bus\/event_bus.dart';  class ListenCubit extends Cubit&lt;String> {   ListenCubit(this.eventBus) : super('Start') {     subscription = eventBus.on&lt;CounterIsEven>().listen((event) {     emit(event.data);   }); }    final EventBus eventBus;   late final StreamSubscription subscription;      @override   Future&lt;void> close() {     subscription.cancel();     return super.close();   } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0427\u0442\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u043e\u0441\u044c? \u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0435 \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 CounterCubit \u2014 \u043c\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 EventBus. <strong>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0448\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u043c.<\/strong> \u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u0430\u043a Singleton \u0432 DI \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0436\u0434\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0435 CounterEvent (\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u2014 \u043a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0447\u0435\u0442\u043d\u043e\u0435). \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u043f\u043e \u0448\u0438\u043d\u0435, \u043d\u0430\u0441 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442.<\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c main.dart:<\/p>\n<details class=\"spoiler\">\n<summary>main.dart<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"dart\">import 'package:flutter\/material.dart'; import 'package:flutter_bloc\/flutter_bloc.dart'; import 'package:mitap_event_bus\/cubits\/counter_cubit\/counter_cubit.dart'; import 'package:mitap_event_bus\/cubits\/listen_cubit\/listen_cubit.dart'; import 'package:mitap_event_bus\/event_bus\/event_bus.dart';  import 'event_bus\/event.dart';  \/\/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439 EventBus eventBus = EventBus();  void main() {   runApp(MaterialApp(     theme: ThemeData(     colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),     useMaterial3: true,     ),     home: const App())); }   class App extends StatelessWidget { const App({super.key});    @override   Widget build(BuildContext context) {     return MultiBlocProvider(       providers: [         BlocProvider(       create: (context) => CounterCubit()),         BlocProvider(       create: (context) => ListenCubit(eventBus)),       ],       child: _CounterScreenView());   } }   class _CounterScreenView extends StatelessWidget {   @override   Widget build(BuildContext context) {     return Scaffold(       floatingActionButton: FloatingActionButton(       onPressed: () {         context.read&lt;CounterCubit>().increment();         \/\/\/ \u041f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0438\u0306 \u0441\u043c\u0435\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u0448\u0438\u043d\u0443         \/\/\/ \u0435\u0441\u043b\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0447\u0435\u0442\u043d\u044b\u0438\u0306         final counter = context.read&lt;CounterCubit>().state;         if (counter.isEven) {           eventBus.addEvent(CounterIsEven(counter));         }},       child: const Icon(Icons.add)),       appBar: AppBar(         backgroundColor: Theme.of(context).colorScheme.inversePrimary,         ),         body: Padding(         padding: const EdgeInsets.all(16),         child: Stack(         children: [           const SizedBox(height: 30),           BlocBuilder&lt;ListenCubit, String>(             builder: (context, state) {               return Text(                 state.toString(),                 style: Theme.of(context).textTheme.headlineMedium,               );             },           ),           Center(             child: Column(             mainAxisAlignment: MainAxisAlignment.center,             children: &lt;Widget>[               const SizedBox(height: 30),               BlocBuilder&lt;CounterCubit, int>(                 builder: (context, state) {                   return Text(            <\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-357739","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357739","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=357739"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357739\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}