簡體   English   中英

Flutter Bloc 依賴項的多個偵聽器 - 接收先前的狀態

[英]Multiple listeners of a Flutter Bloc dependency - receiving previous states

這是我的 Flutter 應用程序 MultiBlocProvider 設置;

MultiBlocProvider(
      providers: [
        BlocProvider<LocationBloc>(create: (BuildContext context) {
          return LocationBloc();
        }),

        BlocProvider<AddressBloc>(create: (BuildContext context) {
          return AddressBloc(
              location: BlocProvider.of<LocationBloc>(context)
                ..add(LocationStarted()))
            ..add(AddressStarted());
        }),

        BlocProvider<CampaignBloc>(
          create: (BuildContext context) => CampaignBloc(
              addressBloc: BlocProvider.of<AddressBloc>(context),
              repo: CampaignsRepository())
            ..add(CampaignsInitial()),
        ),

        BlocProvider<BusinessBloc>(
          create: (BuildContext context) => BusinessBloc(
              addressBloc: BlocProvider.of<AddressBloc>(context),
              repo: BusinessRepository())
            ..add(BusinessInitial()),
        )
      ],
      child: MaterialApp(...)
          ,
    )

LocationBloc 從位置服務獲取位置。 AddressBloc 從 LocationBloc 獲取更新的位置並將其轉換為地址。

CampaignBloc 在其構造函數中偵聽 AddressBloc stream 以獲取地址(位置)更改,並為更改的位置發出活動。

CampaignBloc({required this.repo, required this.addressBloc})
      : super(CampaignState()) {
    _addressSubscription =
        addressBloc.stream.asBroadcastStream().listen((AddressState state) {
      if (state is AddressLoadSuccess) {
        Location location = state.location;

        add(CampaignChangedLocation(location: location));
      }
    });
  }

BusinessBloc 在其構造函數中執行相同的操作,並且(應該)為更改的位置發出業務。

 BusinessBloc({required this.repo, required this.addressBloc})
      : super(BusinessInitial()) {
    _addressSubscription =
        addressBloc.stream.asBroadcastStream().listen((AddressState state) {
      if (state is AddressLoadSuccess) {
        Location location = state.location;

        add(BusinessChangedLocation(location: location));
      }
    });
  }

HomeView 有一個 BlocBuilder<CampaignBloc, CampaignState> 用於構建活動列表。

AlliesView 有一個 BlocBuilder<BusinessBloc, BusinessState> 用於構建企業列表。

CampaignBloc 在構建 HomeView 時正在接收更新的位置,但在轉換到 AlliesView 時,AddressBloc stream 上的偵聽器沒有接收到更新的位置,因為它在事件發生后訂閱了 stream。 如何在 AddressBloc stream 的后續偵聽器中獲取更新的位置?

我制定了一個解決方案 - 使用 rxdart 的 ReplaySubject。 所以在 AddressBloc 我覆蓋#stream

  ReplaySubject<AddressState>? s;
  @override
  get stream {
    if (s == null) {
      s = new ReplaySubject<AddressState>();
      Stream upstream = super.stream;
      upstream.listen((value) {
        s!.add(value);
      });
    }
    return s!;
  }

現在,當我創建訂閱 AddressBloc 的 BusinessBloc 時,我得到了 AddressBloc stream 上的先前狀態。

MultiBlocListener(
  listeners: [
    BlocListener<BlocA, BlocAState>(
      listener: (context, state) {},
    ),
    BlocListener<BlocB, BlocBState>(
      listener: (context, state) {},
    ),
    BlocListener<BlocC, BlocCState>(
      listener: (context, state) {},
    ),
  ],
  child: ChildA(),
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM