简体   繁体   English

flutter_bloc many Event to many BlocBuilder

[英]flutter_bloc many Event to many BlocBuilder

Recently I am learning flutter_bloc , and I refer to the project flutter_weather .最近在学习flutter_bloc ,参考项目flutter_weather

What I am puzzled is that if a Bloc class has many Events, and most of the Event s will have values returned by State , and there are many BlocBuilder s in the project, what should I do if I want a BlocBuilder to only respond to a certain Event ?令我疑惑的是,如果一个Bloc类有很多 Events,并且大部分Event都会有State返回的值,而项目中有很多BlocBuilder ,我想要一个BlocBuilder只响应怎么办某个Event

The method I can think of is to divide this Bloc into multiple Bloc s, or treat each value to be returned as an attribute of Bloc , BlocBuilder uses the buildwhen method to determine whether to rebuild.我能想到的方法是把这个Bloc分成多个Bloc ,或者把每个要返回的值当作Bloc一个属性, BlocBuilder使用buildwhen方法来判断是否重建。

But both of these methods are not good for me.但是这两种方法都不适合我。 Is there any good method?有什么好的方法吗? It is best to have projects on github for reference. github上最好有项目供参考。

For example: This is Event:例如:这是事件:

abstract class WeatherEvent extends Equatable {
  const WeatherEvent();
}

class WeatherRequested extends WeatherEvent {
  final String city;

  const WeatherRequested({@required this.city}) : assert(city != null);

  @override
  List<Object> get props => [city];
}

class WeatherRefreshRequested extends WeatherEvent {
  final String city;

  const WeatherRefreshRequested({@required this.city}) : assert(city != null);

  @override
  List<Object> get props => [city];
}

This is State:这是状态:

abstract class WeatherState extends Equatable {
  const WeatherState();

  @override
  List<Object> get props => [];
}

class WeatherInitial extends WeatherState {}

class WeatherLoadInProgress extends WeatherState {}

class WeatherLoadSuccess extends WeatherState {
  final Weather weather;

  const WeatherLoadSuccess({@required this.weather}) : assert(weather != null);

  @override
  List<Object> get props => [weather];
}

class WeatherLoadFailure extends WeatherState {}

This is Bloc:这是块:

class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
  final WeatherRepository weatherRepository;

  WeatherBloc({@required this.weatherRepository})
      : assert(weatherRepository != null),
        super(WeatherInitial());

  @override
  Stream<WeatherState> mapEventToState(WeatherEvent event) async* {
    if (event is WeatherRequested) {
      yield* _mapWeatherRequestedToState(event);
    } else if (event is WeatherRefreshRequested) {
      yield* _mapWeatherRefreshRequestedToState(event);
    }
  }

  Stream<WeatherState> _mapWeatherRequestedToState(
    WeatherRequested event,
  ) async* {
    yield WeatherLoadInProgress();
    try {
      final Weather weather = await weatherRepository.getWeather(event.city);
      yield WeatherLoadSuccess(weather: weather);
    } catch (_) {
      yield WeatherLoadFailure();
    }
  }

  Stream<WeatherState> _mapWeatherRefreshRequestedToState(
    WeatherRefreshRequested event,
  ) async* {
    try {
      final Weather weather = await weatherRepository.getWeather(event.city);
      yield WeatherLoadSuccess(weather: weather);
    } catch (_) {}
  }
}

This is BlocConsumer:这是 BlocConsumer:

// BlocBuilder1
BlocBuilder<WeatherBloc, WeatherState>(
  builder: (context, state) {
    if (state is WeatherLoadInProgress) {
      return Center(child: CircularProgressIndicator());
    }
    if (state is WeatherLoadSuccess) {
      final weather = state.weather;
      return Center(child: Text("WeatherRequested "))
    }
)
// BlocBuilder2
BlocBuilder<WeatherBloc, WeatherState>(
  builder: (context, state) {
    if (state is WeatherLoadInProgress) {
      return Center(child: CircularProgressIndicator());
    }
    if (state is WeatherLoadSuccess) {
      final weather = state.weather;
      return Center(child: Text("WeatherRefreshRequested"))
    }
)

The problem is that I want BlocBuilder1 only to work when the type of Event is WeatherRequested and BlocBuilder2 only works when the type of Event is WeatherRefreshRequested.问题是我希望 BlocBuilder1 仅在事件类型为 WeatherRequested 时工作,而 BlocBuilder2 仅在事件类型为 WeatherRefreshRequested 时工作。 One of my ideas is that each Event has its own State, and then judge the type of State in buildwhen.我的一个想法是每个Event都有自己的State,然后在buildwhen中判断State的类型。

Is there any good method?有什么好的方法吗?

if you want to build you widget to respond for certain states you should use BlocConsumer and tell that bloc in buildWhen to tell it what state it should build/rebuild you widget on.如果您想构建您的小部件以响应某些状态,您应该使用BlocConsumer并在buildWhen 中告诉该集团以告诉它应该构建/重建您的小部件的状态。

BlocConsumer<QuizBloc, QuizState>(
  buildWhen: (previous, current) {
    if (current is QuizPoints)
      return true;
    else
      return false;
  },
  listener: (context, state) {},
  builder: (context, state) {
    if (state is QuizPoints)
      return Container(
        child: Center(
          child: Countup(
            begin: 0,
            end: state.points,
            duration: Duration(seconds: 2),
            separator: ',',
          ),
        ),
      );
    else
      return Container();
  },
);

暂无
暂无

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

相关问题 flutter_bloc 为许多集团共享 state - flutter_bloc share state for many blocs 如果您在 BlocBuilder 中指定 bloc,flutter_bloc 会处理 cubit 吗? - Does flutter_bloc dispose of the cubit if you specify the bloc in the BlocBuilder? flutter_bloc-如果没有BlocBuilder,如何获得价值? - flutter_bloc - how can i get value without BlocBuilder? flutter_bloc BlocBuilder 没有重建,也没有在 BlocObserver 中观察到状态变化 - flutter_bloc BlocBuilder not rebuilding as well state change not observed in BlocObserver 请评论 3 flutter_bloc writing styles: BlocBuilder, BlocListener, BlocConsumer - Please comment on 3 flutter_bloc writing styles: BlocBuilder, BlocListener, BlocConsumer 在 flutter_bloc 的 bloc 中添加事件的问题 - problem in adding event in the bloc in flutter_bloc 在 flutter_bloc 中调用异步事件 - Calling async event in flutter_bloc flutter_bloc的BlocBuilder是否可以避免重建未更改的Widget部分? - Can BlocBuilder of flutter_bloc avoid rebuild part of Widget which not changing? 一个构建函数返回null,有问题的小部件是:BlocBuilder<NotificationBloc, NotificationState> 在 flutter_bloc 包中 - A build function returned null,The offending widget is: BlocBuilder<NotificationBloc, NotificationState> in flutter_bloc package 从 v7.2.1 迁移到 flutter_bloc v 8.0.0 后,不会触发 flutter_bloc 事件 - flutter_bloc event is not trigger after Migrate to flutter_bloc v 8.0.0 from v7.2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM