New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best approach for obtaining two different return types response model for our data source? #2
Comments
I did not understand what you meant. Could you please provide more details? |
I need to make two API calls to the same remote data source, but each API will provide a different response - one for User and one for UserType. Both responses correspond to different models. For example:
How can I accomplish this using the same data source file? Should I create separate remote data sources for each response? |
There is no need to define any new data sources. You can use the current user data source. It is not necessary to define a separate data source for each model class. Once you define the model class (UserType), you can apply code lines to various layers of the architecture. Upon examining the code, I don't think it will be very challenging. You need to modify the repository (abstract and concrete), remote data source, and presentation parts. |
Can you provide me with some code snippets of how I can do this if we have multiple responses in the same remote data source? In order to quickly solve my work, I have created two data sources, Cubit and Repository, for each response type to handle the case. However, this approach may not be suitable if I have a larger number of APIs on the same data source. If I maintain the same method for one module with 20 API calls, I will have to produce distinct data sources, repositories, and cubits, which could become a problem in the future. Therefore, I am posting this question here to find a more general solution. |
Place the codes related to the api call inside the UserRemoteDataSourceImpl class itself:
Also, the repository classes should be changed as follows.
In fact, in this solution, we ignore the Helper classes ApiHelper and RepositoryHelper, and we must write the corresponding methods in the child classes instead of Helper classes. and we don't need to define any additional datasources. |
I would suggest deleting the classes ApiHelper and RepositoryHelper for the sake of simplicity and rewriting the code accordingly. These classes are generic helper classes that have been written to reduce code duplication. Removing them can make it easier for you to understand the code. |
Do we need a cubit file separate to receive two different response data?
I am getting this error while write the
How to solve this issue on the cubit file? If I removed "User" from here GenericCubit then it creates a problem at the view level. How can one identify the API calls that a cubit state generates for various response models? |
Can we utilize API helper and repository helper to handle multiple responses? Implementing a helper class would simplify potential future issues. Currently, writing the code for each call could cause problems if modifications are necessary. |
As I mentioned earlier, the same thing that should be done for ApiHelper and RepositoryHelper should also be done for GenericCubit. That is, the code related to UserType should be transferred from GenericCubit to UserCubit. as follows:
The getSingleItem method is defined in the GenericCubit and is only capable of retrieving a User object, not a UserType object. |
Yes it is possible. There are several solutions
But I do not recommend this solution. because you have to define type parameter for each model class.
|
After invoking getUserType() and listening on a BlocListener, I received a null value. How to fix this problem? |
It seems that you intended to use HTTP method 'GET', which means that some data should be returned from the server. However, your code does not match with the 'GET' method. It may be useful for other HTTP methods, but not for 'GET'. Please modify your code accordingly.
|
I apologize for my mistake. As a newcomer to flutter and flutter state management, I sometimes become confused. Thank you for helping me identify my mistake. I am facing another issue. After implementing the updated code, I am able to successfully call both the User and UserType APIs. The User API is called first, followed by the UserType API. However, when I try to retrieve the data from both APIs on the next screen, I encounter an issue: the cubit only displays the data from the last API call. What is the best way to handle this situation? Currently, I am storing the data in a variable and passing it to the next screen. Is this a good approach or is there a better way to handle this?
I have removed the To display data on the next screen, I am storing the data from the first API call in the |
I believe that if you post your problem on Stack Overflow or this Telegram group, you are more likely to receive a faster response. Yes, you can. In general, there are different solutions available that can be used depending on your requirements. • The easiest way is passing data through constructor. • The bloc class in Flutter acts as a central storage. This means that by defining a variable in the bloc and storing data in it, you can always retrieve updated data in all the screens that use that bloc. • Another solution is to define a variable or getter inside the state class of the bloc (Generic state) and retrieve it in the desired screen. Your solution appears to be correct, but I am unsure about the reason for the error since your code has been modified from the original code in the repository. |
Clean Architecture Version (Cubit)/lib/features/comment/data/datasources
Suppose we have two distinct response models for the same data source, how can we obtain them? Is it necessary to create a new data_source dart file for each new model, or can we organize multiple response models using the same data source?
The text was updated successfully, but these errors were encountered: