Xor96
Xor96

Reputation: 432

Firestore query doesn't return nearby locations with Geo flutter fire

I'm trying get the nearby location. Here, Imagine I'm in Beverly hills and in my firestore I have stored the Latitude and Longitude of Los Angeles. Both places has a distance of 12 miles = 19.3 KM. In GeoFlutterFire I have provided 10Km as radius with Beverly Hills as center. But still it provides the data of LA.

Here is the code

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final geo = Geoflutterfire();
  final _firestore = FirebaseFirestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: geoFlut(),
      ),
    );
  }

  geoFlut() {
    GeoFirePoint myLocation = geo.point(latitude: 34.0522, longitude: 118.2437);
    _firestore
        .collection('locations')
        .add({'name': 'LA', 'position': myLocation.data});

    GeoFirePoint center = geo.point(latitude: 34.072165, longitude: 118.402624);

    var collectionReference = _firestore.collection('locations');

    double radius = 10;
    String field = 'position';

    Stream<List<DocumentSnapshot>> stream = geo
        .collection(collectionRef: collectionReference)
        .within(center: center, radius: radius, field: field);

    stream.listen((List<DocumentSnapshot> documentList) {
      // print(stream.length);
      print('Here');
      print(documentList[0].data());
    });
  }
}

What's wrong with this Code? Help me sort this out!

Upvotes: 0

Views: 1515

Answers (1)

Xor96
Xor96

Reputation: 432

Yeah, after many error I sorted it Out. The problem is with query that I haven't used strict mode after adding strictmode: true. The problem came to an end.

Upvotes: 1

Related Questions