felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.8k stars 3.39k forks source link

Question: Implementing other packages to the suggested architecture #3116

Closed Arthur-Zaripov closed 2 years ago

Arthur-Zaripov commented 2 years ago

Hello, Felix! According to Domain-Driven Design there are 4 application layers: Domain, Infrastructure, Application, Presentation. My BloCs are located in the Application layer. I hope this is correct.

But the problem is that in practice, a lot of packages from "pub.dev" use methods that should directly access to Infrastructure layer without using Application layer (BloC) . For example, there is a popular package flutter_typeahead . This is like a TextFormField, where you can show suggestions (autocomplete) to users as they type.

TypeAheadField(
  suggestionsCallback: (value) async {
    return await UserRepository.getFiltredUsers(value);// Directly access to Infrastructure layer from Presentation layer. Is it good?
  },
  itemBuilder: (context, suggestion) {
    return ListTile(
      leading: Icon(Icons.person),
      title: Text(suggestion['name']),
     ),
    );
  },
  onSuggestionSelected: (suggestion) {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => UserPage(user: suggestion)
    ));
  },
)

I could not implement this functionality using BloC, because the suggestions shows in an "Overlay" that floats on top of other widgets. This package is good, but it access directly from the Presentation layer to the Infrastructure layer.

But there are often situations when it is easier to directly access from Presentation to Infrastructure layer without using Bloc (Application layer).

So i have 2 questions:

  1. Is it a good practice (in certain situations) to access directly from the Presentation layer to the Infrastructure layer without using a BloC (Application layer)?
  2. How to use Overlay widgets with BloC state managment? Google search doesn't help me. If you will give an example, that would be great.

Thank you for your attention!

Gene-Dana commented 2 years ago

👋🏼 Hi there! I really appreciate you taking the time to not only learn this approach and use BLoC, but also the time you took to raise an issue and ask a question.

I'm not sure the best way to rectify this situation with flutter_typeahead. If you are bent on using it, it seems that you may have to break away from the suggested architecture.

What I suggest instead is to check out the flowbuilder location example from another package that Felix sourced which uses bloc/cubit to achieve the same results. Perhaps you can derive inspiration from this example and apply it to your own application !

Closing this for now as its regarding a package that we do not support, although feel free to reach out to the BloC Discord community with this question, specifically the architecture channel, for the fastest response, or to ask any follow up questions here in the issue