Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to sort my list of cards by the latitude and longitude I have set as fields in Firestore to the user's location in Flutter (by their latitude and longitude). Does anyone know how to do this? My code below in the sort function only reversed the order of my cards by descending order of the documents in my database:

import 'dart:async';
import 'package:geolocator/geolocator.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:swiftbee/Cards/Merchant Card.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class BakeryList extends StatefulWidget {

  @override
  _BakeryListState createState() => _BakeryListState();
}

class _BakeryListState extends State<BakeryList> {


  StreamSubscription<QuerySnapshot> subscription;
  List bakeryList;

  final CollectionReference collectionReference = Firestore.instance.collection('bakeries');

  Geolocator geolocator = Geolocator();

  Position userLocation;


  Future<Position> getLocation() async {
    var currentLocation;
    try {
      currentLocation = await geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.best);
    } catch (e) {
      currentLocation = null;
    }
    return currentLocation;
  }

@override
  void initState() {
    super.initState();

    subscription = collectionReference.snapshots().listen((data) {
      setState(()  {
        bakeryList = data.documents;
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    return bakeryList != null ?
    ListView.builder(

        itemCount: bakeryList.length,
        itemBuilder: (context, index) {


          String imgPath = bakeryList[index].data['image'];
          String merchantTextPath = bakeryList[index].data['name'];
          String locationNamePath = bakeryList[index].data['location'];

          double latitudePath = bakeryList[index].data['latitude'];
          double longitudePath = bakeryList[index].data['longitude'];
          String geoPath = '$latitudePath, $longitudePath';


          void distanceCalc() async {
            userLocation = await getLocation();

            final double myPositionLat = userLocation.latitude;
            final double myPositionLong = userLocation.longitude;

            final double Lat = bakeryList[index].data['latitude'];
            final double Long = bakeryList[index].data['longitude'];

            final DistanceInMetres = await Geolocator().distanceBetween(myPositionLat, myPositionLong, Lat, Long)/1000;
            final Time = DistanceInMetres / 12 * 60;

            bakeryList.sort((a, b){
              return DistanceInMetres.toInt();
            });
          }

          distanceCalc();



          return BakeryCard(
            etaText: '',
            locationText: locationNamePath,
            merchantText: bakeryTextPath,
            assetImage: Image.network(imgPath),

            function: (){});
        })
        : Center(child: CircularProgressIndicator(),
);

  }
}


What I have tried:

Tried to sort it by using the compareTo function between two strings of '$myPositionLat, $myPositionLong' and '$geoPath'. That didn't work either.
Posted
Updated 24-Jan-22 12:35pm

1 solution

Iam having the same problem , did you know how to make it?
 
Share this answer
 
Comments
Richard Deeming 25-Jan-22 4:22am    
"Me too" is not a solution to this question.

Either click the "Have a Question or Comment?" button under the question and post a comment, or ask your own question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900