Firestore 查询不会 return 附近有 Geo flutter fire 的位置
Firestore query doesn't return nearby locations with Geo flutter fire
我正在尝试获取附近的位置。在这里,假设我在比佛利山庄,并且在我的 firestore 中存储了洛杉矶的纬度和经度。两个地方相距 12 英里 = 19.3 公里。在 GeoFlutterFire 中,我提供了 10Km 作为半径,以 Beverly Hills 为中心。不过还是提供了LA的数据。
这是代码
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());
});
}
}
这段代码有什么问题?帮我解决这个问题!
是的,经过多次错误后我将其整理出来。问题是在添加 strictmode: true
后我还没有使用 strict mode 的查询。问题告一段落。
我正在尝试获取附近的位置。在这里,假设我在比佛利山庄,并且在我的 firestore 中存储了洛杉矶的纬度和经度。两个地方相距 12 英里 = 19.3 公里。在 GeoFlutterFire 中,我提供了 10Km 作为半径,以 Beverly Hills 为中心。不过还是提供了LA的数据。
这是代码
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());
});
}
}
这段代码有什么问题?帮我解决这个问题!
是的,经过多次错误后我将其整理出来。问题是在添加 strictmode: true
后我还没有使用 strict mode 的查询。问题告一段落。