I know asking questions doesn't belong here but I am searching for weeks and I couldn't find answer anywhere and no one responded on stack overflow.
I have a tabview where I have popular, recent and upcoming categories. They all have same response on the api. I am trying to fetch data from api using flutter_bloc. Previously I was using rxdart subject and I made a subject for each type of data. Now using flutter bloc I want to achieve the same. What I want to do is to switch between the tabs. Previously I used behaviorsubject to hold the data until next event but now I want to transition to bloc pattern. How do I achieve same type of result using flutter_bloc? Or I need to create bloc for each type? And finally how can I fetch data from api such that when tab is switched, state is persisted? My Rxdart implementation:
class DataBloc {
final DataRepo _repository = DataRepo();
final BehaviorSubject<Data> _recent = BehaviorSubject<Data>();
final BehaviorSubject<Data> _popular = BehaviorSubject<Data>();
final BehaviorSubject<Data> _upcoming = BehaviorSubject<Data>();
getData(String type) async {
Data response = await _repository.getData(type);
if (type == "recent") {
_recent.sink.add(response);
} else if (type == "upcoming") {
_upcoming.sink.add(response);
} else {
_popular.sink.add(response);
}
}
dispose() {
_recent?.close();
_popular?.close();
_upcoming?.close();
}
BehaviorSubject<Data> get recent => _recent;
BehaviorSubject<Data> get popular => _popular;
BehaviorSubject<Data> get upcoming => _upcoming;
}
Hi @Z3rolive I think you might read the https://bloclibrary.dev/#/architecture to find how to integrate flutter_bloc in easy mode. However, I麓m going to explain how I would do in this case:
abstract DataBlocState{}
class FetchingRecentTab extends DataBlocState{}
class FetchedRecentTab extends DataBlocState{}
class ErrorFetchingRecentTab extends DataBlocState{}
class FetchingPopularTab extends DataBlocState{}
class FetchedPopularTab extends DataBlocState{}
And have a blocbuilder with each case in each screen.
I hope that you will understand the bloc pattern! greetings!
I have used bloc pattern before and I am somewhat familiar with it. But in this case I need to switch tabs while calling their individual api. So in initstate I pass initial event which translates to loading indicator. It is fine for the first time but when user calls api again, auction initial event is added again which causes the state to be loading state again. That's where I am having problems. I have 3 api which are all paginated. I have to switch between them while maintaining their state.
So, I think you need two blocs, one for the state of the UI (If user is in one tab or press another tab) and other to manage the state of calls to the API (if data have been loaded or is fetching) and the DataBloc need to listen UIBloc to know which data have to fetch and dataBloc always have the information loaded so if the user change the page, the dataBloc have the old data that will be sent to the ui if the user come back to the tab.
Can you provide a simple example maybe? I think I know what you mean but I don't exactly understand what I have to do to implement two blocs.
I can give you a pseudocode to follow the guidelines to implement:
I hope that this will be useful for your implementation, greetings!
Thanks a lot. I am still somewhat confused but I can't expect to be walked through line by line code. I'll give it a try and I think I can do it now. If I run into unfixable issue, I'll maybe open it back up but for now I am closing and trying on my own :)
Most helpful comment
I can give you a pseudocode to follow the guidelines to implement:
I hope that this will be useful for your implementation, greetings!