简体   繁体   中英

Flutter: listen data from Bloc state

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));
}

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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM