RJASSI98
RJASSI98

Reputation: 23

Sharing entities between features in Flutter clean architecture

I am trying to develop a small application in Flutter with BloC state management and get_it dependency injection while using clean architecture.

I currently have developed a feature which makes an API request to an endpoint and maps that information to a model which is then accessed through bloc builder state as an entity.

I followed this guide to understand clean architecture so my project is structured the same way.

Below is how I am defining the entity in the presentation layer and assigning it the values from bloc state:

static LoginEntity? loginEntity = const LoginEntity();
loginEntity = state.loginEntity![0];

In my bloc state I have loginEntity defined:

abstract class RemoteLoginDetailsState extends Equatable {
  const RemoteLoginDetailsState({this.loginEntity});

  @override
  List<Object?> get props => [loginEntity!];
}

In my api service.g.dart file my request is made and mapped to the model:

    LoginModel value = LoginModel.fromJson(_result.data!);
    final httpResponse = HttpResponse([value], _result);
    return httpResponse;

I am having issues understanding how I can use the information obtained from the API response in one feature in other features while adhering to clean architecture.

How do we properly share information from entities after it has been mapped?

Say I have a login feature which has a name object and I obtain the value of that name object and map it to my model. How should I then access the value of the name object in another feature while adhering to clean architecture?

I am aware maybe it can be done through state by passing the state from one feature to another but I am unsure if this still adheres to clean architecture and how exactly it should be done.

I understand DTO's can also be used but I always thought they should only be used for transferring objects in the same feature not from one feature to another.

Upvotes: 1

Views: 586

Answers (1)

FaBotch
FaBotch

Reputation: 298

Say I have a login feature which has a name object and I obtain the value of that name object and map it to my model. How should I then access the value of the name object in another feature while adhering to clean architecture?

For classic scenarios/feature, I will suggest you to fetch a fresh data from API for each feature and display or manipulate it as you want for the specific feature. It will ensure that the data is up to date.

If the data is persistant and does not change, you should consider save in local storage

If you update the data, you should have an endpoint to do that.

With Stream you could catch the update data and have reactive UI

Upvotes: 0

Related Questions