如何从B-BLoC函数调用A-BLoC函数?

时间:2019-04-12 01:39:46

标签: dart flutter firebase-analytics bloc

因此,像大多数人一样,我是Flutter,Bloc和Firebase Analytics的新手。有一种方法可以让一个人听另一个人来同步bloc,但这与我的情况完全不符,因为我想跟踪一些“与Analytics(分析)相关的状态变量”。

AnalyticsBloc extends Bloc<AnalyticsEvent, AnalyticsState> {
  final FirebaseAnalytics analytics;
  final FirebaseAnalyticsObserver observer;

  @override
    FirebaseAnalyticsState get initialState => FirebaseAnalyticsState.initial();

  void setScreen(String currentScreen) {
    // sends and stores the currentScreen in Bloc State
    dispatch(SetScreenEvent(currentScreen));
  }

  void sendEvent(String eventName) {
    // uses the stored currentScreen in Bloc State
    dispatch(SendEventEvent(eventName));
  }
  ...
}
class AppBloc extends Bloc<AppEvent, AppState> {
  @override
  AppState get initialState => AppState.initial();

  void someApplicationEvent() {
    // Problem: is there any way to trigger sendEvent from this Bloc?
    sendEvent('someAppEventTriggered');
    dispatch(ResetAppEvent());
  }
}

问题:有什么方法可以从AnalyticsBloc.sendEvent触发AppBloc吗?

还是应该以不同的方式设计它?

1 个答案:

答案 0 :(得分:0)

将blocA实例传递给blocB构造函数并调用blocA函数