我已经在我的距离函数中打印了totalTime,它会产生所有DocumentSnapshot值,这些值来自于我在函数中创建的计算出的距离变量:
Performing hot restart...
Syncing files to device iPhone 11 Pro Max...
Restarted application in 1,761ms.
flutter: 5.631018616945024
flutter: 2.6736733762697478
flutter: 0.6686528422968158
flutter: 1.7422156404871896
flutter: 9.861003284651066
但是在构建方法的ListView.builder的etaText中打印的结果仅返回9.861003284651066(最终的DocumentSnapshot结果)。当我打印出“时间”(在距离函数中等于“ totalTime”)时,它将返回:
flutter: 9.861003284651066
flutter: 9.861003284651066
flutter: 9.861003284651066
flutter: 9.861003284651066
flutter: 9.861003284651066
如果任何人都可以在这里解释我在做什么错,我将不胜感激!谢谢。
import 'dart:async';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:geolocator/geolocator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:myApp/Cards/Store Card.dart';
class StoreList extends StatefulWidget {
@override
_StoreListState createState() => _StoreListState();
}
class _StoreListState extends State<StoreList> {
StreamSubscription<QuerySnapshot> subscription;
List storeList;
final CollectionReference collectionReference = Firestore.instance.collection('stores');
Geolocator geolocator = Geolocator();
bool sort = true;
double time;
Future getLocation() async {
var currentLocation;
try {
currentLocation = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium);
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
double calculateDistance(lat1, lon1, lat2, lon2){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
return 12742 * asin(sqrt(a));
}
double distance(Position position, DocumentSnapshot snapshot) {
final double myPositionLat = position.latitude;
final double myPositionLong = position.longitude;
final double lat = snapshot.data['latitude'];
final double long = snapshot.data['longitude'];
double totalTime = calculateDistance(myPositionLat, myPositionLong, lat, long) / 8 * 5 * 60 / 35;
if(totalTime >= 40){
return totalTime;
} else setState(() {
time = totalTime;
});
print(totalTime);
return totalTime;
}
void sortDistance(){
subscription = collectionReference.snapshots().listen((data) async {
final location = await getLocation();
final documents = data.documents.where((snapshot) => distance(location, snapshot) <= 40).toList();
// documents.sort((a, b) {
// final distanceA = distance(location, a);
// final distanceB = distance(location, b);
// return distanceA.compareTo(distanceB);
// });
setState(() {
storeList = documents;
});
});
}
@override
void initState() {
super.initState();
sortDistance();
}
@override
void dispose() {
subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return storeList != null ?
ListView.builder(
itemCount: storeList.length,
itemBuilder: (context, index) {
print(time);
String imgPath = storeList[index].data['image'];
String storeTextPath = storeList[index].data['name'];
String locationNamePath = storeList[index].data['location'];
return StoreCard(
etaText: time,
locationText: locationNamePath,
storeText: storeTextPath,
assetImage: Image.network(imgPath),
function: (){},
);
})
: Center(child: CircularProgressIndicator());
}
}