# Getting Started with the BLoC Pattern | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449>\
**Category:** 🎓 Courses & Articles\
**Created:** [August 30, 2019, 2:38pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449 "2019-08-30T14:38:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![teekol](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/teekol/32/96057_2.png) [@teekol](https://forums.kodeco.com/u/teekol)\
**Post date:** [August 30, 2019, 2:38pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/1 "2019-08-30T14:38:24Z")

</div>

See how to use the popular BLoC pattern to architect your Flutter app and manage the flow of data through your widgets using Dart streams.

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/4074597-getting-started-with-the-bloc-pattern](https://www.raywenderlich.com/4074597-getting-started-with-the-bloc-pattern)

---

<div class="post-metadata">

**Author:** ![spgms](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/spgms/32/111854_2.png) [@spgms](https://forums.kodeco.com/u/spgms)\
**Post date:** [September 6, 2019, 10:32pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/2 "2019-09-06T22:32:30Z")

</div>

Regarding your following note, do you have an example of the best practice in Flutter?

> _Note:_ It’s a best practice for production apps to not store your API keys in source code or in your VCS. Better to read them in from a config file that is read in when your app is built.

---

<div class="post-metadata">

**Author:** ![teekol](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/teekol/32/96057_2.png) [@teekol](https://forums.kodeco.com/u/teekol)\
**Post date:** [September 7, 2019, 1:28am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/3 "2019-09-07T01:28:25Z")

</div>

That’s actually an interesting question. I don’t think there is an official best practice. You have to decide what works best for your team.

In the case of this tutorial, since the code is open source, its probably not a good idea to publicly share your api keys. The keys that Zomato generates have a limit on how many calls they can make in a day, so it is theoretically possible that if I published my API key, the tutorial would stop working.

A simple solution would be to have a dart file where you write your keys that is added to your .gitignore. This would effectively be a the same as having a local.properties file

On internal projects where the code is not open source, this is less of an issue. It might even cause more annoyances for your team to since there will be extra hoops they’ll have to jump through to get their environment set up. For these kind of projects I would just try to avoid committing your deployment keys and keep those separately in a safe place.

At the end of the day, just have a conversion with your team to decide what strategy works best.

---

<div class="post-metadata">

**Author:** ![spgms](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/spgms/32/111854_2.png) [@spgms](https://forums.kodeco.com/u/spgms)\
**Post date:** [September 7, 2019, 9:38pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/4 "2019-09-07T21:38:46Z")

</div>

Creating the dart file and .gitignore was exactly what I was thinking, but I wasn’t sure if it was a good idea. I’ve not worked with API keys in my existing iOS apps, so I’ve not had to deal with it (my “team” is currently a team of 1).

---

<div class="post-metadata">

**Author:** ![mattnz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mattnz/32/81494_2.png) [@mattnz](https://forums.kodeco.com/u/mattnz)\
**Post date:** [September 25, 2019, 8:42pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/5 "2019-09-25T20:42:02Z")

</div>

Guys, nice tutorial. Well done.  
Are you going to be doing a similar one on Provider? I feel BLoC is a steep learning curve for new beginners to Flutter where Provider might be easier to understand.

---

<div class="post-metadata">

**Author:** ![teekol](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/teekol/32/96057_2.png) [@teekol](https://forums.kodeco.com/u/teekol)\
**Post date:** [September 27, 2019, 8:26pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/6 "2019-09-27T20:26:16Z")

</div>

Hi Mattnz. You are correct, BLoC is certainly not a beginner architecture. When you are first getting started with Flutter and just learning your way around widget composition, BLoC should probably be the last thing on your mind.

We do have some other tutorials in progress about Provider and ScopedModel that will be published in a few months. Stay tuned!

One aspect of this tutorial that is probably even more important than BLoC is the concept of layering. With this structure you can use BLoC, Provider, Redux, MVVM, whatever. As long as you are keeping a clean separation between your view layer and data layer, you’ll be off to the races. I think its more important to focus on a clean separation of concerns and then chose more advanced design patterns based on the requirements of your project.

---

<div class="post-metadata">

**Author:** ![aenima](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aenima/32/81494_2.png) [@aenima](https://forums.kodeco.com/u/aenima)\
**Post date:** [November 5, 2019, 8:26am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/7 "2019-11-05T08:26:31Z")

</div>

Hi Brian,  
following the Google best pratices about BLoC pattern, you should never expose a bloc method making it public, as I have understood the only allowed inputs and outputs are streams and sinks.  
I have seen that you are using methods such as  
`locationBloc.selectLocation(location);`  
Am I missing something?  
Many thanks

---

<div class="post-metadata">

**Author:** ![sivacherukuri](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sivacherukuri/32/102075_2.png) [@sivacherukuri](https://forums.kodeco.com/u/sivacherukuri)\
**Post date:** [November 13, 2019, 7:45am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/8 "2019-11-13T07:45:09Z")

</div>

BackboneJS application to Flutter migration. Which design pattern to use?

We have developed an application using BackboneJS. Now we are planning to migrate this project using Flutter.

Our Project folder structure is like below

[![enter image description here](https://i.stack.imgur.com/ElZdK.png)](https://i.stack.imgur.com/ElZdK.png)

Navigation from one view to another happening at root level, sub-levels(Eg: Jobs, Modules in the above screenshot). We have to trigger notifications between multiple classes.

We are planning to support all 3 platforms (iOS, Android and Web)

Could some one suggest me the best architecture or design pattern to implement this BackbnoneJS application in flutter.

---

<div class="post-metadata">

**Author:** ![chris\_coder](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/chris_coder/32/87353_2.png) [@chris\_coder](https://forums.kodeco.com/u/chris_coder)\
**Post date:** [December 20, 2019, 7:52pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/9 "2019-12-20T19:52:17Z")

</div>

It’s an excellent article, a minor detail I notice is that you are not following dart naming convention, any reason for doing that? [Effective Dart: Style | Dart](https://dart.dev/guides/language/effective-dart/style#do-name-libraries-and-source-files-using-lowercase_with_underscores)  
naming packages and files with lower case and underscore, etc.

---

<div class="post-metadata">

**Author:** ![teekol](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/teekol/32/96057_2.png) [@teekol](https://forums.kodeco.com/u/teekol)\
**Post date:** [December 20, 2019, 9:16pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/10 "2019-12-20T21:16:24Z")

</div>

Hi Chris. Thank for your feedback. You are correct, this is technically an error. This is no good reason why Dart file naming convention was not followed in this article, other than the fact that is was an accidental oversight. All future Flutter articles on our site will make sure to follow Dart’s convention.

---

<div class="post-metadata">

**Author:** ![developerlibs](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/developerlibs/32/81494_2.png) [@developerlibs](https://forums.kodeco.com/u/developerlibs)\
**Post date:** [January 2, 2020, 6:00am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/11 "2020-01-02T06:00:54Z")

</div>

Bloc pattern is good for [Asynchronous programming in Dart](https://www.developerlibs.com/2019/05/flutter-how-architect-bloc-pattern-exampe.html).

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 11, 2020, 5:16pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/12 "2020-01-11T17:16:23Z")

</div>

@developerlibs Thank you for sharing this - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![knarfdev](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/knarfdev/32/95000_2.png) [@knarfdev](https://forums.kodeco.com/u/knarfdev)\
**Post date:** [February 18, 2020, 8:25pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/13 "2020-02-18T20:25:10Z")

</div>

Flutter newbie here! I was going thru an older tutorial on the same subject and they discussed having their BlocProvider rely on InheritedWidget for performance. Does this tutorial take that into account or is that no longer an issue with how Flutter/Dart has evolved?

---

<div class="post-metadata">

**Author:** ![fleepgeek](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fleepgeek/32/108464_2.png) [@fleepgeek](https://forums.kodeco.com/u/fleepgeek)\
**Post date:** [February 19, 2020, 2:07pm UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/14 "2020-02-19T14:07:35Z")

</div>

Yes `InheritedWidget` is better for performance because finding the widget with the bloc in the parent tree is an O(1) operation. The downside to using an `InheritedWidget` is that it does not have a `dispose` method which is necessary for disposing blocs in order to prevent memory leaks.

A `Stateful` widget has a dispose method so that solves the problem and the `ancestorWidgetOfExactType` method of the `BuildContext` class was used to access the bloc up in the tree.  
The downside to this approach is that the operation is O(n) but this was the approach used in the tutorial.

Now, you might be asking so whats the solution since both of them have downsides.  
Well, you could combine the best of both worlds to solve this problem.  
You’ll need to use an `InhheritedWidget` to pass the bloc down and return it inside a `Stateful` widget so the bloc can be disposed.  
I know that sounds intimidating but it is fairly simple 🙂

You could also use the provider package for the dependency injection if you want to use a library.

Note: `ancestorWidgetOfExactType` has been deprecated after Flutter v1.12.1

@shogunkaramazov i’m new on the video team and i don’t know if i could provide links to outside materials where i show this or would the tutorial be updated?

Just need to know how things work.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 1, 2020, 6:02am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/15 "2020-04-01T06:02:57Z")

</div>

@teekol Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![teekol](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/teekol/32/96057_2.png) [@teekol](https://forums.kodeco.com/u/teekol)\
**Post date:** [April 1, 2020, 8:31am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/16 "2020-04-01T08:31:21Z")

</div>

@fleepgeek Thank you for your feedback. I’ve updated the article to reflect the api depreciation.

In terms of the question of performance, yes I agree that `findAncestorWidgetOfExactType` is not the most efficient methods and the Flutter documentation even points this out. But in practice, this has turned out to be a mostly moot point. You can certainly go much further in optimizing this widget. However the article was already quite long so I chose to keep with the naive approach so that the focus didn’t shift away from core concepts the article was trying to teach.

We could always write a follow up article on optimizing Flutter, but it when it comes to that topic it is critically important to never pre-optimize. We should only spend time fixing this if it become a noticeable performance issue and is causing jank in your app.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 1, 2020, 9:23am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/17 "2020-04-01T09:23:18Z")

</div>

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 1, 2020, 9:23am UTC](https://forums.kodeco.com/t/getting-started-with-the-bloc-pattern-raywenderlich-com/80449/18 "2020-04-01T09:23:20Z")

</div>

Closed on April 1, 2020, 9:23am UTC


