{"id":51,"date":"2024-01-11T16:57:03","date_gmt":"2024-01-11T19:57:03","guid":{"rendered":"https:\/\/renatokuroe.com.br\/?p=51"},"modified":"2024-01-11T17:03:44","modified_gmt":"2024-01-11T20:03:44","slug":"how-to-use-bloc-architecture","status":"publish","type":"post","link":"https:\/\/renatokuroe.com.br\/?p=51","title":{"rendered":"How to use BLoC Architecture?"},"content":{"rendered":"\n<p>This example assumes that you are using the <code>flutter_bloc<\/code> package, which is a popular package for managing state in Flutter applications.<\/p>\n\n\n\n<p>First, make sure to add the <code>flutter_bloc<\/code> package to your <code>pubspec.yaml<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-262b2cab3d0f0caf437df068472a9655\"><code>dependencies:\n  flutter:\n    sdk: flutter\n  flutter_bloc: ^7.0.0<\/code><\/pre>\n\n\n\n<p>Then, create a new file called <code>my_bloc.dart<\/code> and add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-57596982e6e38466c4a3782012481b20\"><code>import 'package:flutter_bloc\/flutter_bloc.dart';\n\n\/\/ Events\nabstract class MyEvent {}\n\nclass IncrementEvent extends MyEvent {}\n\nclass DecrementEvent extends MyEvent {}\n\n\/\/ States\nclass MyState {\n  final int counter;\n\n  MyState(this.counter);\n}\n\n\/\/ BLoC\nclass MyBloc extends Bloc&lt;MyEvent, MyState> {\n  MyBloc() : super(MyState(0));\n\n  @override\n  Stream&lt;MyState> mapEventToState(MyEvent event) async* {\n    if (event is IncrementEvent) {\n      yield MyState(state.counter + 1);\n    } else if (event is DecrementEvent) {\n      yield MyState(state.counter - 1);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul>\n<li>We have two events (<code>IncrementEvent<\/code> and <code>DecrementEvent<\/code>) representing actions that can trigger state changes.<\/li>\n\n\n\n<li>We have a simple state (<code>MyState<\/code>) with a counter value.<\/li>\n\n\n\n<li>The <code>MyBloc<\/code> class extends <code>Bloc&lt;MyEvent, MyState&gt;<\/code> and overrides the <code>mapEventToState<\/code> method to handle events and emit new states accordingly.<\/li>\n<\/ul>\n\n\n\n<p>To use this BLoC in your Flutter widget, you would typically wrap your widget with a <code>BlocProvider<\/code> and access the BLoC using <code>BlocBuilder<\/code> or <code>BlocListener<\/code>. Here&#8217;s a simple example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-ebb433e203d9798ce7bd06500ddf1553\"><code>import 'package:flutter\/material.dart';\nimport 'package:flutter_bloc\/flutter_bloc.dart';\n\nvoid main() => runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      home: BlocProvider(\n        create: (context) => MyBloc(),\n        child: MyHomePage(),\n      ),\n    );\n  }\n}\n\nclass MyHomePage extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: Text('BLoC Example'),\n      ),\n      body: BlocBuilder&lt;MyBloc, MyState>(\n        builder: (context, state) {\n          return Center(\n            child: Column(\n              mainAxisAlignment: MainAxisAlignment.center,\n              children: &#91;\n                Text('Counter: ${state.counter}'),\n                SizedBox(height: 20),\n                ElevatedButton(\n                  onPressed: () {\n                    context.read&lt;MyBloc>().add(IncrementEvent());\n                  },\n                  child: Text('Increment'),\n                ),\n                ElevatedButton(\n                  onPressed: () {\n                    context.read&lt;MyBloc>().add(DecrementEvent());\n                  },\n                  child: Text('Decrement'),\n                ),\n              ],\n            ),\n          );\n        },\n      ),\n    );\n  }\n}<\/code><\/pre>\n\n\n\n<p>This example sets up a simple Flutter app with a BLoC managing the state. The UI updates based on the counter value in the BLoC. The <code>Increment<\/code> and <code>Decrement<\/code> buttons trigger the corresponding events, updating the state and causing the UI to re-render.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example assumes that you are using the flutter_bloc package, which is a popular package for managing state in Flutter applications. First, make sure to add the flutter_bloc package to your pubspec.yaml file: Then, create a new file called my_bloc.dart and add the following code: In this example: To use this BLoC in your Flutter&#8230;<\/p>\n","protected":false},"author":1,"featured_media":54,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":1,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/52"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=\/wp\/v2\/media\/54"}],"wp:attachment":[{"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renatokuroe.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}