我正在使用 Bloc 模式开发 Flutter 应用程序。认证成功后,UserSate就有了User对象。在所有其他 Bloc 中,我需要访问 UserState 中的 User 对象。我尝试在其他 Bloc 的构造函数参数上获取 UserBloc 并访问 User 对象。但它显示 User 对象为 null。有人有更好的解决方案吗?
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 …Run Code Online (Sandbox Code Playgroud)