Flutter flift_bloc多事件对多BlocBuilder

Flutter flift_bloc多事件对多BlocBuilder,flutter,bloc,flutter-bloc,Flutter,Bloc,Flutter Bloc,最近我在学习《颤振集团》,我参考了这个项目 我感到困惑的是,如果一个Bloc类有许多事件,并且大多数Events将有State返回的值,并且在项目中有许多BlocBuilders,如果我希望BlocBuilder只响应某个事件,我该怎么办 我能想到的方法是将这个Bloc划分为多个Blocs,或者将要返回的每个值视为Bloc的属性,BlocBuilder使用buildwhen方法来确定是否重建 但这两种方法对我都不好。有什么好方法吗?最好在github上有项目作为参考 例如: 这是一个事件: ab

最近我在学习《颤振集团》,我参考了这个项目

我感到困惑的是,如果一个
Bloc
类有许多事件,并且大多数
Event
s将有
State
返回的值,并且在项目中有许多
BlocBuilder
s,如果我希望
BlocBuilder
只响应某个
事件,我该怎么办

我能想到的方法是将这个
Bloc
划分为多个
Bloc
s,或者将要返回的每个值视为
Bloc
的属性,
BlocBuilder
使用
buildwhen
方法来确定是否重建

但这两种方法对我都不好。有什么好方法吗?最好在github上有项目作为参考

例如: 这是一个事件:

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];
}
抽象类WeatherEvent扩展了equalable{
const WeatherEvent();
}
类WeatherEvent{
最后的字符串城市;
const weatherrequest({@required this.city}):断言(city!=null);
@凌驾
列表获取道具=>[城市];
}
类WeatherRefreshRequested扩展WeatherEvent{
最后的字符串城市;
const WeatherRefreshRequested({@required this.city}):断言(city!=null);
@凌驾
列表获取道具=>[城市];
}
这是国家:

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 {}
抽象类WeatherState扩展了equalable{
常数WeatherState();
@凌驾
列表获取道具=>[];
}
类WeatherInitial扩展了WeatherState{}
类WeatherLoadInProgress扩展WeatherState{}
类WeatherLoadSuccess扩展了WeatherState{
最终天气;
const WeatherLoadSuccess({@required this.weather}):断言(weather!=null);
@凌驾
列表获取道具=>[天气];
}
类WeatherLoadFailure扩展了WeatherState{}
这是一个集团:

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 (_) {}
  }
}
class WeatherBloc扩展了Bloc{
最终天气知识库天气知识库;
WeatherBloc({@required this.weatherRepository})
:assert(weatherRepository!=null),
super(WeatherInitial());
@凌驾
Stream mapEventToState(WeatherEvent事件)异步*{
如果(请求事件){
收益率*\u映射天气请求状态(事件);
}else if(请求事件){
收益率*(事件);
}
}
流_MapWeatherRequestedState(
天气预报要求的事件,
)异步*{
屈服天气载荷过程();
试一试{
最终天气=wait weatherRepository.getWeather(event.city);
产量WeatherLoadSuccess(天气:weather);
}接住{
屈服载荷失效();
}
}
流_-MapWeatherRefreshRequestedState(
在请求的事件中,
)异步*{
试一试{
最终天气=wait weatherRepository.getWeather(event.city);
产量WeatherLoadSuccess(天气:weather);
}捕获({}
}
}
这是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"))
    }
)
//BlocBuilder1
BlocBuilder(
生成器:(上下文、状态){
如果(状态为WeatherLoadInProgress){
返回中心(子项:CircularProgressIndicator());
}
如果(状态为WeatherLoadSuccess){
最终天气=state.weather;
返回中心(子项:文本(“天气请求”))
}
)
//BlocBuilder2
BlocBuilder(
生成器:(上下文、状态){
如果(状态为WeatherLoadInProgress){
返回中心(子项:CircularProgressIndicator());
}
如果(状态为WeatherLoadSuccess){
最终天气=state.weather;
返回中心(子项:文本(“天气刷新请求”))
}
)
问题是,我希望BlocBuilder1仅在事件类型为WeatherRequested时工作,BlocBuilder2仅在事件类型为WeatherRefreshRequested时工作。我的想法之一是每个事件都有自己的状态,然后判断buildwhen中的状态类型


有什么好方法吗?

如果你想构建一个小部件来响应你应该使用的特定状态 BlocConsumer并在buildWhen中告诉该bloc,告诉它应该在什么状态下构建/重建小部件

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();
  },
);
BlocConsumer(
buildWhen:(上一个,当前){
if(电流为QuizPoints)
返回true;
其他的
返回false;
},
侦听器:(上下文,状态){},
生成器:(上下文、状态){
如果(状态为QuizPoints)
返回容器(
儿童:中心(
孩子:倒计时(
开始:0,
完:state.points,,
持续时间:持续时间(秒数:2),
分隔符:',',
),
),
);
其他的
返回容器();
},
);

您能提供一个代码示例来说明问题所在吗?从文本中解读有点困难。如果您只想对状态中的特定变化做出反应,请使用
BlocBuilder
buildWhen
参数,该参数是一个接受先前和当前状态的函数,并返回一个bool,该bool决定>生成器应运行。