在MultiBlocProvider中添加的某些事件不会被调度Flutter

时间:2020-04-19 10:58:12

标签: flutter bloc

我在SyncBlocMultiBlocProvider包)中添加了一个新的flutter_bloc块,它创建了MapScreen所需的所有块,对于某些块,它还向在屏幕本身中显示数据。 问题是,虽然从MultiBlocProvider本身正确添加了位置事件(针对LocationBloc),但同步事件(针对SyncBloc)却没有。如果我改为从MapScreen的MultiBlocListener中将它们添加为

BlocProvider.of<SyncBloc>(context).add(SyncLanguages());

他们的工作按预期进行,因此看起来SyncBloc正确提供了。您能找出我对较新的SyncBloc做错的事情还是朝正确的方向指出吗? 一如既往,非常感谢您的时间和帮助。

这是main()中的MultiBlocProvider:

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        const AppLocalizationsDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''),
        const Locale('it', ''),
        const Locale('es', ''),
      ],
      localeResolutionCallback:
          (Locale locale, Iterable<Locale> supportedLocales) {
        for (Locale supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode ||
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        return supportedLocales.first;
      },
      debugShowCheckedModeBanner: false,
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is Unauthenticated) {
            return LoginScreen(userRepository: _userRepository);
          }
          if (state is Authenticated) {
            return MultiBlocProvider(
              providers: [
                BlocProvider<TrackingBloc>(
                  create: (context) => TrackingBloc(),
                ),
                BlocProvider<DirectionsBloc>(
                  create: (context) => DirectionsBloc(),
                ),
                BlocProvider<GeoBloc>(
                  create: (context) => GeoBloc(),
                ),
                BlocProvider<RouteBloc>(
                  create: (context) => RouteBloc(),
                ),
                BlocProvider<SchedulerBloc>(
                  create: (context) => SchedulerBloc(),
                ),
                BlocProvider<CheckerBloc>(
                  create: (context) => CheckerBloc(),
                ),
                BlocProvider<LocationBloc>(
                    create: (context) => LocationBloc(
                          mapRepository: _mapRepository,
                        )
                          ..add(GetLocationStream())
                          ..add(GetLocation())
                          ..add(GetIsoLocationUser())),
                BlocProvider<SyncBloc>(
                    create: (context) =>
                        SyncBloc()..add(SyncLanguages())..add(SyncIcons())),
                BlocProvider<AlertBloc>(create: (context) {
                  return AlertBloc(
                    alertRepository: _alertRepository,
                  );
                }),
              ],
              child: MapScreen(
//                mapRepository: _mapRepository,
                user: state.user,
//              alertRepository: FirebaseAlertRepository(),
              ),
            );
          }
          return SplashScreen();
        },
      ),
      navigatorObservers: [
        FirebaseAnalyticsObserver(analytics: analytics),
      ],
    );

SyncEvent:

abstract class SyncEvent {
  const SyncEvent();

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

class SyncLanguages extends SyncEvent {}

class SyncIcons extends SyncEvent {}

和SyncBloc:

class SyncBloc extends Bloc<SyncEvent, SyncState> {
  @override
  SyncState get initialState => InitialState();

  Stream<SyncState> mapEventToState(SyncEvent event) async* {
    if (event is SyncLanguages) {
      print('SyncLanguages received');
    }
    if (event is SyncIcons) {
      print('SyncIcons received');
    }
  }
}

1 个答案:

答案 0 :(得分:1)

问题与默认情况下BlocProvider的create方法是惰性的有关。因此,在调用.of方法之前,BlocProvider不会创建该块。要立即创建块,只需将lazy:参数设置为false。

BlocProvider<LocationBloc>(
                    lazy: false,
                    create: (context) => LocationBloc(
                          mapRepository: _mapRepository,
                        )
                          ..add(GetLocationStream())
                          ..add(GetLocation())
                          ..add(GetIsoLocationUser())),
                BlocProvider<SyncBloc>(
                    lazy: false,
                    create: (context) => SyncBloc()
                      ..add(SyncLanguages())
                      ..add(SyncIcons())),

这实际上是有效的,尽管即使没有将lazy参数设置为false也发送了AuthenticationBloc和LocationBloc事件。仍然要检查为什么,但是我想这两个区块分别由BlocBuilder和BlocListener创建。确定后,我会立即编辑答案。