Flutter 请求颤振中的位置时,BLoC不屈服状态
我使用了三个flatter包来实现一个功能,用户通过拉刷新,使用BLoC逻辑检索地理坐标并传递回flatter 问题是,当我在pull to refresh中发送调用时,我无法让BLoC产生返回结果 地理定位\u bloc.dartFlutter 请求颤振中的位置时,BLoC不屈服状态,flutter,dart,Flutter,Dart,我使用了三个flatter包来实现一个功能,用户通过拉刷新,使用BLoC逻辑检索地理坐标并传递回flatter 问题是,当我在pull to refresh中发送调用时,我无法让BLoC产生返回结果 地理定位\u bloc.dart class GeolocationBloc extends Bloc<GeolocationEvent, GeolocationState> { @override GeolocationState get initialState =&g
class GeolocationBloc extends Bloc<GeolocationEvent, GeolocationState> {
@override
GeolocationState get initialState => GeolocationUninitialized();
@override
Stream<GeolocationState> mapEventToState(GeolocationEvent event) async* {
if (event is RequestLocation) {
yield* _mapGeolocationRequestLocation();
}
}
Stream<GeolocationState> _mapGeolocationRequestLocation() async* {
Position position;
position = await Geolocator().getCurrentPosition();
print("RETRIEVED LOCATION"); // I CAN REACH HERE EVERYTIME.
if (position == null) {
yield LocationLoaded(0, 0);
} else {
yield LocationLoaded(position.latitude, position.longitude);
}
}
@override
void initState() {
super.initState();
_geolocationBloc.dispatch(RequestLocation());
}
void _onRefresh() {
_geolocationBloc.dispatch(RequestLocation());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Username')),
body: BlocProviderTree(
blocProviders: [
BlocProvider<PostBloc>(bloc: _postBloc),
BlocProvider<GeolocationBloc>(bloc: _geolocationBloc),
],
child: BlocListenerTree(
blocListeners: [
BlocListener<GeolocationEvent, GeolocationState>(
bloc: _geolocationBloc,
listener: (BuildContext context, GeolocationState state) {
if (state is LocationLoaded) {
print('LOADED'); // THIS NEVER GETS PRINTED WHEN PULLED TO REFRESH.
lat = state.latitude;
long = state.longitude;
}
..
根据日志,第一次调用打印了检索到的位置和加载到的位置,但是在第二次检索到的位置之后没有其他任何事情发生
如何修复此问题,以使pull to refresh将成功调用BLoC逻辑,而BLoC逻辑将返回具有适当坐标的
LocationLoaded()
对象。我通过从我的状态
和事件
BLoC类中删除equalable
继承来解决此问题<代码>相等相等检查的实施导致BlocListener未对相应的if/else块短路:
i.e. if (state is LocationLoaded)..
在equalatable
中,相等性检查可能更严格,或者因为我正在传递该州的两个坐标(纬度/经度),这会导致相等性检查失败
状态类:
@immutable
abstract class GeolocationState extends Equatable { // Remove inheritance.
}
class LocationLoaded extends GeolocationState {
}
class GeolocationUninitialized extends GeolocationState {
}
第二个检索位置后的位置值是多少?gps位置是否正在更改?
I/flatter(6125):RequestLocation I/flatter(6125):Lat:37.4219983,Long:-122.084 I/flatter(6125):37.4219983 I/flatter(6125):-122.084 I/flatter(6125):检索到的位置
感谢您的回复,我不希望在这种情况下位置发生更改。地理坐标被很好地检索到,但是检索到的值没有被传递{你看到了什么状态值?没有打印任何内容,即没有显示加载的位置。我怀疑if语句根本没有被调用。我曾经有一些事件类的层次结构,并检查它们的顺序,同样的事情发生了。在我的情况下,我需要@override List get props,当child有p时属性。最佳做法是保持“平衡”并覆盖props
@immutable
abstract class GeolocationState extends Equatable { // Remove inheritance.
}
class LocationLoaded extends GeolocationState {
}
class GeolocationUninitialized extends GeolocationState {
}