Flutter 颤振集团。我能';t获取要在mapEventToState()中产生的状态I中传递的值`
我只是从flifter开始,我仍然不确定构造event/state/BLoc/Repositoy类以正确使用此模式的逻辑。当生成一个以值作为输入的状态时,我一直在从bloc中获取Flutter 颤振集团。我能';t获取要在mapEventToState()中产生的状态I中传递的值`,flutter,bloc,Flutter,Bloc,我只是从flifter开始,我仍然不确定构造event/state/BLoc/Repositoy类以正确使用此模式的逻辑。当生成一个以值作为输入的状态时,我一直在从bloc中获取位置值,并将其返回到UI 从存储库开始,我有getLocation()方法,可以从Geolocator locationManager获取坐标: Future<LatLng> getLocation() async { try { locationManager .ge
位置值,并将其返回到UI
从存储库开始,我有getLocation()
方法,可以从Geolocator locationManager
获取坐标:
Future<LatLng> getLocation() async {
try {
locationManager
.getCurrentPosition(
desiredAccuracy: LocationAccuracy.bestForNavigation)
.timeout(Duration(seconds: 5));
} catch (error) {
print(
'getLocation(): error getting current location: ${error.toString()}');
}
}
在屏幕加载时发送到bloc以显示地图,并通过按钮将地图置于用户位置的中心
BlocProvider<MapBloc>(create: (context) {
return MapBloc(mapRepository: MapRepository())..add(GetLocation());
}),
这是基于该状态构建的小部件:
BlocBuilder<MapBloc, MapState>(
bloc: MapBloc(mapRepository: _mapRepository),
builder: (BuildContext context, MapState state) {
final LatLng location = (state as MapLoaded).location;
return Container
在Stream\u mapGetLocationToState()
中,我试图发送add(LocationUpdated(location))
事件,并在Stream mapEventToState
中生成maploadded()
但我找不到任何方法来传递该位置。我还试图在流中直接生成它\u mapGetLocationToState()
,但结果相同
你能看出我做错了什么吗?切换到反应式编程并不是那么容易,但我已经做到了。。这是我第一次尝试这种模式,我还没有完全理解所有的概念,所以我肯定对一些类的想法是错误的。
非常感谢您的时间和帮助,很抱歉问了这么长的问题。
干杯。您的\u mapGetLocationToState()
没有事件作为参数
@override
Stream<MapState> mapEventToState(MapEvent event) async* {
if (event is GetLocation) {
yield* _mapGetLocationToState(event);
}
if (event is GetTracking) {
yield* _mapGetTrackingToState();
}
if (event is LocationUpdated) {
// CANT GET LOCATION TO PASS IN MapLoaded()
yield MapLoaded();
}
}
Stream<MapState> _mapGetLocationToState(GetLocation event) async* {
// now you have event.location
_mapRepository.getLocation();
(location) => add(LocationUpdated(location));
// CANT GET LOCATION TO PASS IN MapLoaded(event.location)
yield MapLoaded(event.location);
}
Stream<MapState> _mapGetTrackingToState() async* {
_mapRepository.setTracking();
}
}
的确非常感谢。如果不将事件类作为输入参数传递,则无法访问事件类参数。有一件事我需要确保我明白。添加(GetLocation事件)
输入参数可以让我直接在\u mapGetLocationToState
中生成MapLoaded(event.location)
,而在mapEventToState
中,我可以生成MapLoaded(event.updatedLocation)
。这是因为location
是GetLocation
事件类的参数,而updatedLocation
是LocationUpdated
事件类的参数吗?再次非常感谢。它不是param,而是field,是的location是GetLocation的字段。我不知道LocationUpdated类,但因为有if(event is locationUpdate),因此事件被标识为LocationUpdated,您应该只能访问LocationUpdated中的字段,而不能访问GetLocationUpdated中的字段!我当时确实做对了。我希望这是像这样的错误(没有分配一些东西),但我担心我没有完全理解模式。从命令式编程切换到声明式/反应式编程需要一些时间,但我最终还是控制住了。感谢您的帮助和术语更正。我真的很感激。很高兴看到Flitter社区和Swift社区一样有帮助。:)好的,别那么快。。现在BlocProvider在..add(GetLocation())中缺少输入。为什么?GetLocation中没有字段。还要求提供StreamSubscriptor。。当我以为我出去了..他们把我拉进来..作为输入传递null
,确实消除了错误。这是坏习惯吗?
BlocBuilder<MapBloc, MapState>(
bloc: MapBloc(mapRepository: _mapRepository),
builder: (BuildContext context, MapState state) {
final LatLng location = (state as MapLoaded).location;
return Container
class MapBloc extends Bloc<MapEvent, MapState> {
final MapRepository _mapRepository;
StreamSubscription _locationStreamSubscription;
MapBloc(
{@required MapRepository mapRepository,
@required StreamSubscription streamSubscription})
: assert(mapRepository != null || streamSubscription != null),
_mapRepository = mapRepository,
_locationStreamSubscription = streamSubscription;
MapState get initialState => MapLoading();
@override
Stream<MapState> mapEventToState(MapEvent event) async* {
if (event is GetLocation) {
yield* _mapGetLocationToState();
}
if (event is GetTracking) {
yield* _mapGetTrackingToState();
}
if (event is LocationUpdated) {
// CANT GET LOCATION TO PASS IN MapLoaded()
yield MapLoaded();
}
}
Stream<MapState> _mapGetLocationToState() async* {
_mapRepository.getLocation();
(location) => add(LocationUpdated(location));
// CANT GET LOCATION TO PASS IN MapLoaded()
yield MapLoaded(l);
}
Stream<MapState> _mapGetTrackingToState() async* {
_mapRepository.setTracking();
}
}
@override
Stream<MapState> mapEventToState(MapEvent event) async* {
if (event is GetLocation) {
yield* _mapGetLocationToState(event);
}
if (event is GetTracking) {
yield* _mapGetTrackingToState();
}
if (event is LocationUpdated) {
// CANT GET LOCATION TO PASS IN MapLoaded()
yield MapLoaded();
}
}
Stream<MapState> _mapGetLocationToState(GetLocation event) async* {
// now you have event.location
_mapRepository.getLocation();
(location) => add(LocationUpdated(location));
// CANT GET LOCATION TO PASS IN MapLoaded(event.location)
yield MapLoaded(event.location);
}
Stream<MapState> _mapGetTrackingToState() async* {
_mapRepository.setTracking();
}
}
BlocProvider<MapBloc>(create: (context) {
return MapBloc(
mapRepository: MapRepository(),
streamSubscription: ...?
)..add(GetLocation(...?));
}),