简体   繁体   English

Flutter:监听来自 Bloc state 的数据

[英]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.只有当我从 bloc 得到一个非空的数据列表时,我才想要显示选项卡。 How can I get access to this?我怎样才能访问这个?

I push events while initState():我在 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.使用BlocBuilder检查数据是否不为空。 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();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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