Загрузка...

How to Properly Call CustomBloc in Flutter's BLoC Pattern

A guide to resolving the common issue of ancestor method calls in Flutter's BLoC implementation, specifically within custom and subclassed blocs.
---
This video is based on the question https://stackoverflow.com/q/68612883/ asked by the user 'Bilal Oleik' ( https://stackoverflow.com/u/16573035/ ) and on the answer https://stackoverflow.com/a/68613519/ provided by the user 'Rahul' ( https://stackoverflow.com/u/16569443/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Child bloc calling its ancestor mapEventToState

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Problem: Calling Ancestor Methods in Flutter BLoC

Using the BLoC (Business Logic Component) pattern in Flutter can significantly enhance your app's architecture by decoupling UI from business logic. However, while extending a custom BLoC, you might encounter an issue regarding the call to super.mapEventToState(). This is particularly common when creating a custom bloc class that will be subclassed by several other blocs to add functionality.

In the following example, we will dive into an issue where a subclassed bloc's events never get dispatched to the ancestor class. Understanding how to correctly call and yield events from a parent BLoC class is critical for robust app development.

Problem Breakdown

Here's a brief overview of the provided code structure:

CustomBloc is a base class that handles common events and which the other blocs, like HomeBloc, will inherit from.

Within HomeBloc, the method super.mapEventToState(event) is intended to call the ancestor class, CustomBloc. However, the expectation is that these events will be dispatched properly, which is not happening.

Code Sample

Consider the original implementation for CustomBloc and its subclass HomeBloc:

[[See Video to Reveal this Text or Code Snippet]]

While the code appears functional, the call to super.mapEventToState(event) lacks the proper syntax to yield the states effectively.

The Solution: Yielding Events Properly

To ensure that the events from HomeBloc are dispatched up to CustomBloc, you need to yield states from the super.mapEventToState() call correctly. This is where many developers get tripped up. Instead of simply calling super.mapEventToState(event), you should use the yield* keyword. This keyword effectively passes the yielded results back to the caller.

Updated HomeBloc Implementation

Here's how you should update the HomeBloc:

[[See Video to Reveal this Text or Code Snippet]]

Key Changes Explained

**Use of yield***: This operator is crucial when you want to delegate yielding states to another generator function (in this case, super.mapEventToState(event)). By doing this, you’re ensuring that all events and states managed by the CustomBloc are appropriately handled and dispatched.

Conclusion

When working with inheritance in Flutter's BLoC pattern, it’s vital to ensure that methods from ancestor classes are invoked correctly, especially when it involves streams of data or states. By following the correct usage of yield*, you facilitate efficient event handling across your custom bloc classes.

This practice not only simplifies your structure but also enhances the manageability of your Flutter applications. Always remember that using yield* allows your subclass to leverage the functionality built into base classes, embodying the true power of the inheritance paradigm in object-oriented programming.

Видео How to Properly Call CustomBloc in Flutter's BLoC Pattern канала vlogize
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки