How can i convert the data from Firestore into dart?


I can access their location from Firestore. What I need is to be able to convert the data from Firestore into dart language. Then I have to do the listing Firestore locations from closest to farthest.
This is my Firestore: enter image description here

This is my code:

import 'dart:math' show cos, sqrt, asin;

import 'package:geolocator/geolocator.dart';

class FavoriteScreen extends StatefulWidget {
  const FavoriteScreen({Key? key}) : super(key: key);
  _FavoriteScreen createState() => _FavoriteScreen();

class _FavoriteScreen extends State<FavoriteScreen> {
  void initState() {

  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));

  Future<Position> getLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    return position;

  Future<double> tryLocation(GeoPoint geoPointLocation) async {
    var location = await getLocation();
    var findAway = calculateDistance(location.latitude, location.longitude,
        geoPointLocation.latitude, geoPointLocation.longitude);


    return Future.value(findAway);

  QuerySnapshot<Map<String, dynamic>> chnageList(
      QuerySnapshot<Map<String, dynamic>> snapshot) {
    for (var i = 0; i <; i++) {
      GeoPoint location =[i].data()["location"];
      double getDistance = 0;
      var aaa = tryLocation(location)
          .then((value) => {getDistance = value, print(getDistance)});

      //item.distance = getDdistance;

    return snapshot;

  final Query query = FirebaseFirestore.instance.collection("1favori");


QuerySnapshot<Map<String, dynamic>> querySnapshot =
       as QuerySnapshot<Map<String, dynamic>>;

            querySnapshot = chnageList(querySnapshot);

            return ListView(
              physics: const BouncingScrollPhysics(),
              children: <Widget>[
                  margin: const EdgeInsets.only(top: 0.0, bottom: 16.8),
                  height: 724.8,
                  child: ListView.builder(
                    itemCount: querySnapshot.size,
                    padding: const EdgeInsets.only(left: 28.8, right: 12),
                    scrollDirection: Axis.vertical,
                    physics: const BouncingScrollPhysics(),
                    itemBuilder: (context, index) {
                      return Container(
                        height: 214.8,
                        width: 188.4,
                        margin: const EdgeInsets.only(right: 16.8, bottom: 50),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(9.6),
                          image: DecorationImage(
                            fit: BoxFit.cover,
                            image: CachedNetworkImageProvider(
                                maxHeight: 200,
                                maxWidth: 200),

How can i list Firestore locations from closest to farthest?
enter image description here


To help the other: The documentation says : "limit() and orderBy() are not supported at the moment. limit() could be used to limit docs inside each hash individually which would result in running limit on all 9 hashes inside the specified radius. orderBy() is first run on geohashes in the library, hence appending orderBy() with another feild wouldn’t produce expected results. Alternatively documents can be sorted on client side."
At the bottom: here

Answered By – mario francois

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published