Home > Net >  Flutter: listen data from Bloc state
Flutter: listen data from Bloc state

Time:12-12

I have tabs in my app:

  • news
  • promotions
  • shop

Also, I have blocs for fetching data for each tab (one bloc per tab). I want show tab only if I get a not empty list of data from bloc. How can I get access to this?

I push events while initState():

void _fetchData() {
context
  ..read<FeedBloc>().add(const FeedEvent.fetch(isFirstFetch: true))
  ..read<PromotionsBloc>().add(const PromotionsEvent.fetch(isFirstFetch: true))
  ..read<ShopBloc>().add(const ShopEvent.fetch(isFirstFetch: true));
}

CodePudding user response:

Use BlocBuilder to check if data is not empty. You can try something like this:

BlocBuilder<FeedBloc, FeedBlocState>(
  builder: (context, state) {
    if(state.news == null || state.news!.isEmpty) return const SizedBox.shrink();
    
    return NewsTab();
  }
  • Related