joscmw95 / geo_firestore

GeoFirestore for Flutter to do location based queries with Firestore
MIT License
10 stars 8 forks source link

List view - dynamic is not a subtype of string #1

Closed victorriveraiii closed 4 years ago

victorriveraiii commented 4 years ago

My goal is to have a map that shows filtered markers from geo firestore, and the same filtered results in listview tiles below the map. I've gotten the markers on the map working perfectly.

populateClients() async { Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high); final queryLocation = GeoPoint(position.latitude, position.longitude); List documents = await geoFirestore.getAtLocation(queryLocation, 10.0); documents.forEach((document) { print(document.data); });

The second part "listview" is what I'm having trouble with. Here is what i've tried:

Here is my submit code:

() async { Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high); await geoFirestore.setLocation((roomnameController.text), GeoPoint(position.latitude, position.longitude));

My next step is to try an inheritable widget but I feel like i'm going to have the same issues, but they will be available everywhere.

Am I approaching this wrong? Did I use geofirestore correctly? Any help is appreciated.

goal1

joscmw95 commented 4 years ago

Hi, GeoFirestore currently does not return data as a stream. Right now it only supports querying a list of documents that is within the radius specified in a parameter.

GeoFirestore.getAtLocation returns Future<List<DocumentSnapshot>> You can render the data in similar fashion as:

List<Map<String, dynamic>> _documents;

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

populateClients() async {
  Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  final queryLocation = GeoPoint(position.latitude, position.longitude);
  List<DocumentSnapshot> snapshots = await geoFirestore.getAtLocation(queryLocation, 10.0);
  final documents = snapshots.map((doc) {
    return doc.data;
  }).toList();
  setState(() {
    _documents = documents;
  });
}

@override
Widget build(BuildContext context) {
  return ListView(
    children: _documents.map((doc) {
      return Row(children: <Widget>[
          Text(doc['title']),
          Text(doc['description']),
        ]);
    }).toList(),
  );
}
victorriveraiii commented 4 years ago

This answer was so helpful. Is it possible to create a new document in firestore from the query? This way I can use the firestore function oncreate() to send message to the results of this query?

joscmw95 commented 4 years ago

Hi, you can add or update documents as you normally would with Firestore and set a geohash location using the helper method geoFirestore.setLocation);. To keep the issue list free of adhoc questions and since this is out of what the issue was opened for, I'm closing this issue.

Note that I'm not actively maintaining this package, there is another more popular GeoFlutterFirestore package, check out GeoFlutterFire.

Feel free to email me if you have any more questions.

victorriveraiii commented 4 years ago

oh, bummer. This was working great so far. i was trying:

populateClientu() async {
  Position position = await Geolocator()
      .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  var queryLocation = GeoPoint(position.latitude, position.longitude);
  List<DocumentSnapshot> snapshots =
      await geoFirestore.getAtLocation(queryLocation, 100.0);
  final documents = snapshots.map((doc) {
    return doc.data;
  }).toList();

  setState(() {
    _documents = documents;
  });

  db.collection('messages').document().setData({
    'token': documents,
  });
}

I'm not sure if it working properly, any advise is appreciated. I looked at GeoFlutterFire and it's not able to do the same things that I use geo_firestore for.