飞镖/飘飘中的队列



我有一个来自firestore的Document快照,它以双精度返回多个值,所以我创建了一个队列变量,将其添加到我的构建方法中。它打印出结果,但当我点击其他页面时,它有一个错误。这个错误打印在控制台中,如下我的代码页所述。当我第一次运行我的应用程序时,这个错误不会出现,但只有当我点击关闭页面时才会出现。

我还注释了sortDance((函数中的排序方法,但当对其进行排序时,文档快照值将不再像卡片那样按排序顺序打印。

如有任何帮助,我们将不胜感激。

这是我的代码:

import 'dart:async';
import 'dart:math';
import 'dart:collection';
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';
import 'package:myapp/MainMenuPages/First%20Menu.dart';
import 'package:myapp/MainMenuPages/Second%20Menu.dart';
import 'package:myapp/MainMenuPages/Third%20Menu.dart';
import 'package:myapp/MainMenuPages/Fourth%20Perkins%20Menu.dart';
import 'package:myapp/MainMenuPages/Fifth%20Menu.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;
Queue timeQueue = Queue();
Queue endTimeQueue = Queue();
Future getLocation() async {
var currentLocation;
try {
currentLocation = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
} 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) * 60 / 28;
double totalFinalTime = calculateDistance(myPositionLat, myPositionLong, lat, long) * 60 / 16;
timeQueue.add(totalTime);
endTimeQueue.add(totalFinalTime);
print(totalTime);
print(totalFinalTime);
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) {

double timeDisplay = timeQueue.removeFirst();
double endTimeDisplay = endTimeQueue.removeFirst();

String imgPath = storeList[index].data['image'];
String storeTextPath = storeList[index].data['name'];
String locationNamePath = storeList[index].data['location'];
return StoreCard(
etaText: timeDisplay.toInt(),
etaText2: endTimeDisplay.toInt(),
locationText: locationNamePath,
storeText: storeTextPath,
assetImage: Image.network(imgPath),
function: (){ if (merchantTextPath == 'First') {
Navigator.pushNamed(context, FirstMenu.id);
} else if (merchantTextPath == 'Second'){
Navigator.pushNamed(context, SecondMenu.id);
} else if (merchantTextPath == 'Third'){
Navigator.pushNamed(context, ThirdMenu.id);
} else if (merchantTextPath == 'Fourth'){
Navigator.pushNamed(context, FourthMenu.id);
} else if (merchantTextPath == 'Fifth'){
Navigator.pushNamed(context, FifthMenu.id);
}
},
);
})
: Center(child: CircularProgressIndicator());
}
}

控制台生成以下内容:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following StateError was thrown building:
Bad state: No element
When the exception was thrown, this was the stack: 
#0      ListQueue.removeFirst (dart:collection/queue.dart:731:25)
#1      _StoreState.build.<anonymous closure> (package:swiftbee/StoreCategories/Store%20List.dart:145:44)
#2      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:15)
#3      SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1260:67)
#4      _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:139:29)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

似乎timeQueue没有任何元素。因此,请执行以下操作:

if(timeQueue.isNotEmpty){
double timeDisplay = timeQueue.removeFirst();
}

最新更新