Reputation: 303
i'm working on an app with firebase , in one screen i need to get a lot of documents at once first image of the day , the stats of user , the pending request , friends stats , and then country stats.
*sorry for my naming btw
class HomeSmileLoading extends HomeState {
const HomeSmileLoading();
@override
List<Object> get props => [];
}
and similar to this i have HomeStatsLoading , friends stats ...
this is how i execute the the code in the start of the app
HomeCubit() : super(HomeInitial()) {
imageOfday();
getStats();
//...
}
i feel like there is better way to do this ..
if anyone have any tips or idea i appreciate your help
Upvotes: 0
Views: 816
Reputation: 5333
Well, it's not wrong what you are doing, just you won't be able to load all the data in parallel. Also, a single Cubit in your case is responsible to handle all the different data you should load - it's not very flexible.
What I would recommend you to do is:
Create a different Cubit for each data source/feature (one for stats, one for requests and so on).
Each Cubit would have different states: Initial, Loading, Loaded, Error - these could be different based on your needs.
When creating/providing Cubits for your app, call the init/load function on each Cubit to load the initial data:
MultiBlocProvider(
providers: [
BlocProvider<StatsCubit>(
create: (BuildContext context) => StatsCubit()..getStats(),
),
BlocProvider<ImageCubit>(
create: (BuildContext context) => ImageCubit()..imageOfday(),
),
BlocProvider<AnyOtherCubit(
create: (BuildContext context) => AnyOtherCubit()..loadData(),
),
],
child: ChildA(),
)
It is a good practice to split Cubits per feature and making them responsible for only a single thing (e.g. handling authentication, user statistics, any specific data, etc.).
Upvotes: 4