我正在尝试计算距离,这是我要做的
@override
Stream<StoreState> mapEventToState(StoresEvent event) async* {
final currentState = state;
if (event is FetchStores) {
yield StoreLoading();
try {
List<Stores> dataChild = await _fetchStores();
yield StoreLoaded(stores: dataChild);
} catch (e) {
yield StoreError(message: e.toString());
}
}
if (event is SortStore) {
print(event);
yield StoreLoading();
if (currentState is StoreLoaded) {
print(currentState);
currentState.stores.forEach((storeData) {
storeData.storeDistance = calculateDistance(event.latitude, event.longitude, storeData.storeLatitude, storeData.storeLongitude);
});
currentState.stores
.sort((a, b) => a.storeDistance.compareTo(b.storeDistance));
yield StoreLoaded(stores: currentState.stores);
}
}
}
这是calculateDistance()
calculateDistance(double userLatitude, double userLongitude,
String storeLatitude, String storeLongitude) async {
var storeDistance = 0.0;
if (userLongitude != 0.0) {
var list = (await Geolocator().distanceBetween(
userLatitude,
userLongitude,
double.parse(storeLatitude),
double.parse(storeLatitude)));
storeDistance = list / 1000;
print(storeDistance.toString());
return storeDistance.toString();
}
}
我怎么称呼它
FloatingActionButton(
onPressed: (){
print(_currentLatitude);
print(_currentLongitude);
storesBloc.add(SortStore(latitude:_currentLatitude, longitude: _currentLongitude));
},
),
对于第一次加载,数据显示正常(FetchStoresEvent()
),但是当我单击FloatingActionButton
(运行SortStore()
事件)时,数据未显示。它停留在yield StoreLoading();
上。我该如何解决?
顺便说一句,这是我从日志中看到的内容
I/flutter (25608): -6.1300483
I/flutter (25608): 106.91085
I/flutter (25608): SortStore
I/flutter (25608): StoreLoaded
I/flutter (25608): 12492.154