Reputation: 329
I have a Document snapshot from firestore and it returns multiple values as a double, so I created a queue variable to add it to my build method. It prints out the results, but it has a bug when I click onto other pages. the bug is printed in the console as stated below my page of code. The bug does not appear when I first run my app, but it only appears when I click off the page.
I have also commented out the sort method in the sortDistance() function, but when that is sorted, the document snapshot values are no longer printed in a sorted order as the cards are.
Any help would be very appreciated.
Here's my code:
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());
}
}
The console produces this:
════════ 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)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Upvotes: 0
Views: 2397
Reputation: 80914
It seems timeQueue
doesn't have any elements. Therefore do the following:
if(timeQueue.isNotEmpty){
double timeDisplay = timeQueue.removeFirst();
}
Upvotes: 1