因此,像大多数人一样,我是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
吗?
还是应该以不同的方式设计它?
答案 0 :(得分:0)
将blocA实例传递给blocB构造函数并调用blocA函数