![]() When mobile app projects gain a certain size, there is an increasingly high probability that screens (in Flutter: widgets) share data across the whole app. Given the assumption that an architectural pattern like BLoC improves the long-term maintainability of software projects, what are the concrete problems it solves? State management Even worse: I have often made the experience that companies decided to rewrite everything because the effort of a huge refactoring would even exceed the effort of a complete rewrite. When a project has reached the point where it has become hardly maintainable, it is a huge load of work to transform it into a well structured project. In my opinion, it’s a good idea to go for the safe approach and cleanly design the architecture right from the beginning as it can save a lot of trouble later on. The problem is: once you start a project, you never know, how big it’s going to become eventually. At this point, a clean architecture will have paid off. However, you will reach a point where the whole code becomes a mess in terms of testability, maintainability and probably also performance. At the beginning you can save quite some time if you go for a very simple approach without too many thoughts about the architecture. No for apps that need to be scalable, readable and maintainable and is worked on by more than one person. The above explanation might sound reasonable but also quite complicated for a problem that can be solved in totally simple ways, right? ![]() It was first mentioned by Google at the Google I/O in 2018 and has since then become the de facto standard for many developers when it comes to state management solutions in Flutter app development. It also aims for reusability of the same logic across multiple widgets. Generally speaking, BLoC ( Business Logic Component) is a design pattern enabling developers to efficiently and conveniently manage state across their apps without a tight coupling between the presentation (view) and the logic. During this article I want to clear things up. However, depending on their personal previous experience with Dart, Flutter, Streams and state management, it might be hard for beginners to wrap their head around it. In response, the bloc emits the state with set likeInfo parameter, reflecting whether the like or unlike happened, and sets the item's name to the corresponding string.It’s almost inevitable to come across the concept of or at least the term “BLoC” when you learn about Flutter and its recommended architecture approaches. When a user taps a like or an unlike icon, an event LikeItem is sent. If some error occurred, we should see a red snack bar with information about that. ![]() If the call is successful, the list should be loaded or updated. In response, the bloc sends the request to the API. Once the user taps a load-more button, an event LoadItems is sent to the bloc. An event to like an item has an order (index) of the item in the list. Similarly, the bloc has 2 events: LoadItems and LikeItem. an optional likeInfo object that holds whether the item was liked or unliked.a bool determining if the loading failed.a list of items where each item contains its name and a liked flag.Users can remove the like by tapping the heart icon again.Īs the app is very simple, it is driven by a single ItemsBloc bloc with single state ItemsState with variables: Liking an item turns it red and makes the snack bar appear. Here, you tap the heart icon next to each item. The second event is a functionality that allows users to like an item. If an error occurs, a red snack bar should appear to inform the user. You simply tap on the load-more button, and if the process succeeds, an updated list with new items is emitted. The first one relates to loading the items. Let's demonstrate a simple app that loads items and lets users like them. In this post, all examples use the Bloc library, particularly the bloc and flutter_bloc packages. In general, any event that should only be shown to the user once without affecting the state falls into this category. Today, we’re going to focus on handling one-time events, such as displaying a confirmation dialog or a snack bar. However, as with every similar tool, it has challenges and can sometimes be difficult to use in specific scenarios. It allows us to separate business logic from the user interface, making managing applications' dynamic and complex states easier. The Flutter BLoC pattern is a popular and efficient way of managing a state in Flutter apps.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |