我正在使用flutter_bloc并利用MultiBlocProvider小部件。一切运行正常,然后在某个时候,我的MultiBlocProvider只是拒绝实例化我创建并添加到其中的任何新Bloc。我对flutter_bloc很陌生,因此任何指针都将有所帮助。
在下面的示例中,在出现此问题之前,我拥有了ItemListsBloc和CartBloc,并且它们继续正确地实例化。这里的问题是LoadFaves集团,以及我尝试添加的任何其他新集团。
任何帮助将不胜感激。
这是我的MultiBlocProvider:
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => ItemListsBloc()..add(LoadItemLists()),
),
BlocProvider(
create: (context) =>
FaveBloc()..add(LoadFaves('3pujK2EPXFaIPue3F42kSMTLgDV2')),
),
BlocProvider(
create: (context) => CartBloc()..add(CartInitialize()),
),
],
child: BlocBuilder<ItemListsBloc, ItemListsState>(
builder: (context, state) {
if (state is ItemListsLoaded) {
return Column(children: [
Categories(items: state.items.values.toList()),
SizedBox(
height: 10.0,
),
Expanded(
child: ListView(
shrinkWrap: true,
children: _buildItemLists(state.itemLists, state.items),
),
)
]);
}
return Container();
},
),
);
}
这是“有问题的”集团:
class FaveBloc extends Bloc<FaveEvent, FaveState> {
final FavesRepository _favesRepository = FavesRepository.instance;
StreamSubscription _favesSubscription;
@override
FaveState get initialState => FaveInitial();
@override
Stream<FaveState> mapEventToState(
FaveEvent event,
) async* {
if (event is LoadFaves) {
yield* _mapLoadFavesToState(event);
} else if (event is UpdateFaves) {
yield* _mapUpdateFavesToState(event);
}
}
Stream<FaveState> _mapLoadFavesToState(LoadFaves event) async* {
_favesSubscription?.cancel();
_favesSubscription = _favesRepository.faves(event.userId).listen(
(faves) => add(UpdateFaves(faves)),
);
}
Stream<FaveState> _mapUpdateFavesToState(UpdateFaves event) async* {
yield FavesUpdated(event.faves);
}
}
和
class OrderBloc extends Bloc<OrderEvent, OrderState> {
final OrderRepository _orderRepository = OrderRepository.instance;
StreamSubscription _ordersSubscription;
StreamSubscription _currOrderSubsction;
@override
OrderState get initialState => OrdersUnitialized();
@override
Stream<OrderState> mapEventToState(
OrderEvent event,
) async* {
if (event is OrderCreated) {
yield* _mapOrderCreatedToState(event);
} else if (event is OrdersUpdated) {
yield* _mapOrderUpdateToState(event);
} else if (event is OrderLoadRequest) {
yield* _mapLoadReqToState();
}
}
Stream<OrderState> _mapLoadReqToState() async* {
_ordersSubscription?.cancel();
_ordersSubscription = _orderRepository.orders().listen(
(orders) => add(OrdersUpdated(orders)),
);
}
Stream<OrderState> _mapOrderCreatedToState(OrderCreated event) async* {
var orderId = await _orderRepository.createNewOrder(event.order);
var state = this.state as OrdersLoadSuccess;
yield state.copyWith(currentOrderId: orderId);
}
Stream<OrderState> _mapOrderUpdateToState(OrdersUpdated event) async* {
yield OrdersLoadSuccess(orders: event.orders);
}
@override
Future<void> close() {
_ordersSubscription?.cancel();
_currOrderSubsction?.cancel();
return super.close();
}
}
非常感谢您
答案 0 :(得分:3)
默认情况下,Bloc是由BlocProvider延迟创建的,这意味着直到通过BlocProvider.of(context)查找该bloc时,才会调用create。您可以在BlocProvider上将lazy设置为false,以强制立即创建块。
BlocProvider(
lazy: false,
create: (_) => MyBloc(),
child: MyChild(),
)
来自FelixA。