如何从另一个 bloc 访问 Bloc 状态的数据
How to access data in Bloc's state from another bloc
我正在使用 Bloc 模式开发 Flutter 应用程序。认证成功后,UserSate 就有了 User 对象。在所有其他 Bloc 中,我需要访问 UserState 中的 User 对象。我尝试在其他 Bloc 的构造函数参数上获取 UserBloc 并访问 User 对象。但它表明用户对象为空。谁有更好的解决方案?
class SectorHomeBloc extends Bloc<SectorHomeEvent, SectorHomeState> {
final OutletRepository outletRepository;
UserBloc userBloc;
final ProductRepository productRepository;
final ProductSubCategoryRepository productSubCategoryRepository;
final PromotionRepository promotionRepository;
final ProductMainCategoryRepository mainCategoryRepository;
SectorHomeBloc({
@required this.outletRepository,
@required this.userBloc,
@required this.productSubCategoryRepository,
@required this.productRepository,
@required this.promotionRepository,
@required this.mainCategoryRepository,
});
@override
SectorHomeState get initialState => SectorHomeLoadingState();
@override
Stream<SectorHomeState> mapEventToState(SectorHomeEvent event) async* {
try {
print(userBloc.state.toString());
LatLng _location = LatLng(
userBloc.state.user.defaultLocation.coordinate.latitude,
userBloc.state.user.defaultLocation.coordinate.longitude);
String _token = userBloc.state.user.token;
if (event is GetAllDataEvent) {
yield SectorHomeLoadingState();
List<Outlet> _previousOrderedOutlets =
await outletRepository.getPreviousOrderedOutlets(
_token, _location, event.orderType, event.sectorId);
List<Outlet> _featuredOutlets =
await outletRepository.getFeaturedOutlets(
_token, _location, event.orderType, event.sectorId);
List<Outlet> _nearestOutlets = await outletRepository.getOutletsNearYou(
_token, _location, event.orderType, event.sectorId);
List<Product> _newProducts = await productRepository.getNewItems(
_token, _location, event.orderType, event.sectorId);
List<Product> _trendingProducts =
await productRepository.getTrendingItems(
_token, _location, event.orderType, event.sectorId);
List<Promotion> _promotions = await promotionRepository
.getVendorPromotions(_token, event.sectorId);
yield SectorHomeState(
previousOrderedOutlets: _previousOrderedOutlets,
featuredOutlets: _featuredOutlets,
nearByOutlets: _nearestOutlets,
newItems: _newProducts,
trendingItems: _trendingProducts,
promotions: _promotions,
);
}
} on SocketException {
yield SectorHomeLoadingErrorState('could not connect to server');
} catch (e) {
print(e);
yield SectorHomeLoadingErrorState('Error');
}
}
}
mapEventToState方法中的打印语句[print(userBloc.state.toString());]显示了UserSate的初始状态。
但是,在执行此代码时,UserState 处于 UserLoggedInState 中。
在文档中有一种官方方法可以做到这一点,称为 Bloc-to-Bloc Communication
这是文档中的示例
class MyBloc extends Bloc {
final OtherBloc otherBloc;
StreamSubscription otherBlocSubscription;
MyBloc(this.otherBloc) {
otherBlocSubscription = otherBloc.listen((state) {
// React to state changes here.
// Add events here to trigger changes in MyBloc.
});
}
@override
Future<void> close() {
otherBlocSubscription.cancel();
return super.close();
}
}
我正在使用 Bloc 模式开发 Flutter 应用程序。认证成功后,UserSate 就有了 User 对象。在所有其他 Bloc 中,我需要访问 UserState 中的 User 对象。我尝试在其他 Bloc 的构造函数参数上获取 UserBloc 并访问 User 对象。但它表明用户对象为空。谁有更好的解决方案?
class SectorHomeBloc extends Bloc<SectorHomeEvent, SectorHomeState> {
final OutletRepository outletRepository;
UserBloc userBloc;
final ProductRepository productRepository;
final ProductSubCategoryRepository productSubCategoryRepository;
final PromotionRepository promotionRepository;
final ProductMainCategoryRepository mainCategoryRepository;
SectorHomeBloc({
@required this.outletRepository,
@required this.userBloc,
@required this.productSubCategoryRepository,
@required this.productRepository,
@required this.promotionRepository,
@required this.mainCategoryRepository,
});
@override
SectorHomeState get initialState => SectorHomeLoadingState();
@override
Stream<SectorHomeState> mapEventToState(SectorHomeEvent event) async* {
try {
print(userBloc.state.toString());
LatLng _location = LatLng(
userBloc.state.user.defaultLocation.coordinate.latitude,
userBloc.state.user.defaultLocation.coordinate.longitude);
String _token = userBloc.state.user.token;
if (event is GetAllDataEvent) {
yield SectorHomeLoadingState();
List<Outlet> _previousOrderedOutlets =
await outletRepository.getPreviousOrderedOutlets(
_token, _location, event.orderType, event.sectorId);
List<Outlet> _featuredOutlets =
await outletRepository.getFeaturedOutlets(
_token, _location, event.orderType, event.sectorId);
List<Outlet> _nearestOutlets = await outletRepository.getOutletsNearYou(
_token, _location, event.orderType, event.sectorId);
List<Product> _newProducts = await productRepository.getNewItems(
_token, _location, event.orderType, event.sectorId);
List<Product> _trendingProducts =
await productRepository.getTrendingItems(
_token, _location, event.orderType, event.sectorId);
List<Promotion> _promotions = await promotionRepository
.getVendorPromotions(_token, event.sectorId);
yield SectorHomeState(
previousOrderedOutlets: _previousOrderedOutlets,
featuredOutlets: _featuredOutlets,
nearByOutlets: _nearestOutlets,
newItems: _newProducts,
trendingItems: _trendingProducts,
promotions: _promotions,
);
}
} on SocketException {
yield SectorHomeLoadingErrorState('could not connect to server');
} catch (e) {
print(e);
yield SectorHomeLoadingErrorState('Error');
}
}
}
mapEventToState方法中的打印语句[print(userBloc.state.toString());]显示了UserSate的初始状态。 但是,在执行此代码时,UserState 处于 UserLoggedInState 中。
在文档中有一种官方方法可以做到这一点,称为 Bloc-to-Bloc Communication 这是文档中的示例
class MyBloc extends Bloc {
final OtherBloc otherBloc;
StreamSubscription otherBlocSubscription;
MyBloc(this.otherBloc) {
otherBlocSubscription = otherBloc.listen((state) {
// React to state changes here.
// Add events here to trigger changes in MyBloc.
});
}
@override
Future<void> close() {
otherBlocSubscription.cancel();
return super.close();
}
}