From a3095febaef1e3fd6db059cde1e36c1464e84b4e Mon Sep 17 00:00:00 2001 From: rajput-abdullah Date: Tue, 11 Mar 2025 12:07:55 +0000 Subject: [PATCH] Updated cloud firestore. --- example/lib/main.dart | 102 +++++++++-------- example/lib/streambuilder_test.dart | 115 +++++++++++-------- example/pubspec.yaml | 4 +- pubspec.lock | 167 +++++++++++++++++++--------- pubspec.yaml | 4 +- 5 files changed, 240 insertions(+), 152 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 0307b8b..9fe7fe2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -23,16 +23,19 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - GoogleMapController _mapController; - TextEditingController _latitudeController, _longitudeController; + GoogleMapController? _mapController; + late TextEditingController _latitudeController, _longitudeController; - // firestore init + // Firestore initialization final _firestore = FirebaseFirestore.instance; - GeoFlutterFire geo; - Stream> stream; + late GeoFlutterFire geo; + late Stream>>> stream; final radius = BehaviorSubject.seeded(1.0); Map markers = {}; + double _value = 20.0; + String _label = ''; + @override void initState() { super.initState(); @@ -42,21 +45,25 @@ class _MyAppState extends State { geo = GeoFlutterFire(); GeoFirePoint center = geo.point(latitude: 12.960632, longitude: 77.641603); stream = radius.switchMap((rad) { - var collectionReference = _firestore.collection('locations'); -// .where('name', isEqualTo: 'darshan'); - return geo.collection(collectionRef: collectionReference).within( - center: center, radius: rad, field: 'position', strictMode: true); - - /* - ****Example to specify nested object**** - - var collectionReference = _firestore.collection('nestedLocations'); -// .where('name', isEqualTo: 'darshan'); - return geo.collection(collectionRef: collectionReference).within( - center: center, radius: rad, field: 'address.location.position'); - - */ + var collectionReference = _firestore + .collection('locations') + .withConverter>( + fromFirestore: (snapshot, _) => snapshot.data()!, + toFirestore: (value, _) => value, + ); + return geo + .collection(collectionRef: collectionReference) + .within( + center: center, + radius: rad, + field: 'position', + strictMode: true, + ) + .map((docs) => docs + .map((doc) => doc as DocumentSnapshot>) + .toList()); }); + } @override @@ -76,20 +83,15 @@ class _MyAppState extends State { title: const Text('GeoFlutterFire'), actions: [ IconButton( - onPressed: _mapController == null - ? null - : () { - _showHome(); - }, + onPressed: _mapController == null ? null : _showHome, icon: Icon(Icons.home), ) ], ), floatingActionButton: FloatingActionButton( onPressed: () { - Navigator.of(context).push(MaterialPageRoute(builder: (context) { - return StreamTestWidget(); - })); + Navigator.of(context) + .push(MaterialPageRoute(builder: (context) => StreamTestWidget())); }, child: Icon(Icons.navigate_next), ), @@ -124,7 +126,7 @@ class _MyAppState extends State { value: _value, label: _label, activeColor: Colors.blue, - inactiveColor: Colors.blue.withOpacity(0.2), + inactiveColor: Colors.blue.withValues(alpha: 0.2), onChanged: (double value) => changed(value), ), ), @@ -173,7 +175,7 @@ class _MyAppState extends State { MaterialButton( color: Colors.amber, child: const Text( - 'Add nested ', + 'Add nested', style: TextStyle(color: Colors.white), ), onPressed: () { @@ -192,16 +194,15 @@ class _MyAppState extends State { void _onMapCreated(GoogleMapController controller) { setState(() { _mapController = controller; -// _showHome(); - //start listening after map is created - stream.listen((List documentList) { + // Start listening after map is created + stream.listen((List>> documentList) { _updateMarkers(documentList); }); }); } void _showHome() { - _mapController.animateCamera(CameraUpdate.newCameraPosition( + _mapController?.animateCamera(CameraUpdate.newCameraPosition( const CameraPosition( target: LatLng(12.960632, 77.641603), zoom: 15.0, @@ -211,14 +212,15 @@ class _MyAppState extends State { void _addPoint(double lat, double lng) { GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng); - _firestore - .collection('locations') - .add({'name': 'random name', 'position': geoFirePoint.data}).then((_) { + _firestore.collection('locations').add({ + 'name': 'random name', + 'position': geoFirePoint.data, + }).then((_) { print('added ${geoFirePoint.hash} successfully'); }); } - //example to add geoFirePoint inside nested object + // Example to add GeoFirePoint inside a nested object void _addNestedPoint(double lat, double lng) { GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng); _firestore.collection('nestedLocations').add({ @@ -244,21 +246,25 @@ class _MyAppState extends State { }); } - void _updateMarkers(List documentList) { - documentList.forEach((DocumentSnapshot document) { - Map snapData = document.data(); - final GeoPoint point = snapData['position']['geopoint']; - _addMarker(point.latitude, point.longitude); - }); + void _updateMarkers(List>> documentList) { + for (var document in documentList) { + final data = document.data(); + if (data != null) { + final positionData = data['position'] as Map?; + if (positionData != null) { + final geoPoint = positionData['geopoint'] as GeoPoint?; + if (geoPoint != null) { + _addMarker(geoPoint.latitude, geoPoint.longitude); + } + } + } + } } - double _value = 20.0; - String _label = ''; - - changed(value) { + void changed(double value) { setState(() { _value = value; - _label = '${_value.toInt().toString()} kms'; + _label = '${_value.toInt()} kms'; markers.clear(); }); radius.add(value); diff --git a/example/lib/streambuilder_test.dart b/example/lib/streambuilder_test.dart index 07d7864..8fee99a 100644 --- a/example/lib/streambuilder_test.dart +++ b/example/lib/streambuilder_test.dart @@ -9,63 +9,89 @@ class StreamTestWidget extends StatefulWidget { } class _StreamTestWidgetState extends State { - Stream> stream; - final _firestore = FirebaseFirestore.instance; - final geo = GeoFlutterFire(); + late Stream>>> stream; + final FirebaseFirestore _firestore = FirebaseFirestore.instance; + final GeoFlutterFire geo = GeoFlutterFire(); // ignore: close_sinks - var radius = BehaviorSubject.seeded(1.0); + final BehaviorSubject radius = BehaviorSubject.seeded(1.0); + + double _value = 20.0; + String _label = ''; @override void initState() { super.initState(); final center = geo.point(latitude: 12.960632, longitude: 77.641603); - stream = radius.switchMap((rad) { - var collectionReference = _firestore.collection('locations'); + stream = radius.switchMap((double rad) { + var collectionReference = _firestore.collection('locations').withConverter>( + fromFirestore: (snapshot, _) => snapshot.data() ?? {}, + toFirestore: (value, _) => value, + ); + // GeoFlutterFire returns a stream of List>. + // Map each document to the correct type. return geo.collection(collectionRef: collectionReference).within( - center: center, radius: rad, field: 'position', strictMode: true); + center: center, + radius: rad, + field: 'position', + strictMode: true, + ).map((docs) => docs.map((doc) => doc as DocumentSnapshot>).toList()); }); } + @override + void dispose() { + _latitudeController.dispose(); + _longitudeController.dispose(); + radius.close(); + super.dispose(); + } + + final TextEditingController _latitudeController = TextEditingController(); + final TextEditingController _longitudeController = TextEditingController(); + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Column( children: [ - StreamBuilder( - stream: stream, - builder: (BuildContext context, - AsyncSnapshot> snapshots) { - if (snapshots.connectionState == ConnectionState.active && - snapshots.hasData) { - print('data ${snapshots.data}'); - return Container( - height: MediaQuery.of(context).size.height * 2 / 3, - child: ListView.builder( - itemBuilder: (context, index) { - final doc = snapshots.data[index]; - Map data = doc.data(); - print( - 'doc with id ${doc.id} distance ${data['distance']}'); - GeoPoint point = data['position']['geopoint']; - return ListTile( - title: Text( - doc.id, - style: const TextStyle(fontWeight: FontWeight.bold), - ), - subtitle: Text('${point.latitude}, ${point.longitude}'), - trailing: Text( - '${data['documentType'] == DocumentChangeType.added ? 'Added' : 'Modified'}'), - ); - }, - itemCount: snapshots.data.length, - ), - ); - } else { - return const Center(child: CircularProgressIndicator()); - } - }, + Expanded( + child: StreamBuilder>>>( + stream: stream, + builder: (BuildContext context, + AsyncSnapshot>>> snapshots) { + if (snapshots.connectionState == ConnectionState.active && snapshots.hasData) { + print('data ${snapshots.data}'); + return Container( + height: MediaQuery.of(context).size.height * 2 / 3, + child: ListView.builder( + itemCount: snapshots.data!.length, + itemBuilder: (context, index) { + final doc = snapshots.data![index]; + // Force non-null since we expect documents to contain data. + final Map data = doc.data()!; + print('doc with id ${doc.id} distance ${data['distance']}'); + // Assume that data['position'] is a Map with a 'geopoint' key. + final GeoPoint point = (data['position'] as Map)['geopoint'] as GeoPoint; + return ListTile( + title: Text( + doc.id, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + subtitle: Text('${point.latitude}, ${point.longitude}'), + trailing: Text( + data['documentType'] == DocumentChangeType.added ? 'Added' : 'Modified', + ), + ); + }, + ), + ); + } else { + return const Center(child: CircularProgressIndicator()); + } + }, + ), ), Padding( padding: const EdgeInsets.only(top: 8.0), @@ -76,7 +102,7 @@ class _StreamTestWidgetState extends State { value: _value, label: _label, activeColor: Colors.blue, - inactiveColor: Colors.blue.withOpacity(0.2), + inactiveColor: Colors.blue.withValues(alpha: 0.2), onChanged: (double value) => changed(value), ), ), @@ -85,14 +111,11 @@ class _StreamTestWidgetState extends State { ); } - double _value = 20.0; - String _label = ''; - - changed(value) { + void changed(double value) { setState(() { _value = value; print(_value); - _label = '${_value.toInt().toString()} kms'; + _label = '${_value.toInt()} kms'; }); radius.add(value); } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index e7b2e05..11a63f4 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,7 +3,7 @@ description: Demonstrates how to use the Geoflutterfire2 plugin. publish_to: "none" environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ^3.6.1 dependencies: flutter: @@ -20,7 +20,7 @@ dev_dependencies: geoflutterfire2: path: ../ - cloud_firestore: ^4.4.4 + cloud_firestore: ^5.6.5 google_maps_flutter: ^2.2.5 # For information on the generic Dart part of this file, see the diff --git a/pubspec.lock b/pubspec.lock index 01fc86a..f95e7a6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,93 +5,106 @@ packages: dependency: transitive description: name: _flutterfire_internals - url: "https://pub.dartlang.org" + sha256: "7fd72d77a7487c26faab1d274af23fb008763ddc10800261abbfb2c067f183d5" + url: "https://pub.dev" source: hosted - version: "1.0.17" + version: "1.3.53" async: dependency: transitive description: name: async - url: "https://pub.dartlang.org" + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" + url: "https://pub.dev" source: hosted - version: "2.9.0" + version: "2.11.0" boolean_selector: dependency: transitive description: name: boolean_selector - url: "https://pub.dartlang.org" + sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + url: "https://pub.dev" source: hosted - version: "2.1.0" + version: "2.1.1" characters: dependency: transitive description: name: characters - url: "https://pub.dartlang.org" + sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" + url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.3.0" clock: dependency: transitive description: name: clock - url: "https://pub.dartlang.org" + sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf + url: "https://pub.dev" source: hosted version: "1.1.1" cloud_firestore: dependency: "direct main" description: name: cloud_firestore - url: "https://pub.dartlang.org" + sha256: "6b5d0ca6b62830ca5bcf98c5e0c882df32dea3cb523b635db3626333e1c29090" + url: "https://pub.dev" source: hosted - version: "4.4.4" + version: "5.6.5" cloud_firestore_platform_interface: dependency: transitive description: name: cloud_firestore_platform_interface - url: "https://pub.dartlang.org" + sha256: "149c7d2d634178aff8e25eba6cbbbc76bd92f37e0e63727a31952c72bbcb77fb" + url: "https://pub.dev" source: hosted - version: "5.11.4" + version: "6.6.5" cloud_firestore_web: dependency: transitive description: name: cloud_firestore_web - url: "https://pub.dartlang.org" + sha256: "05a3c02a7edb3fadeb3f14f491c3a0bbad3ea2c9f22842acbf1d73bceeb93e77" + url: "https://pub.dev" source: hosted - version: "3.3.4" + version: "4.4.5" collection: dependency: transitive description: name: collection - url: "https://pub.dartlang.org" + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf + url: "https://pub.dev" source: hosted - version: "1.16.0" + version: "1.19.0" fake_async: dependency: transitive description: name: fake_async - url: "https://pub.dartlang.org" + sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + url: "https://pub.dev" source: hosted version: "1.3.1" firebase_core: dependency: transitive description: name: firebase_core - url: "https://pub.dartlang.org" + sha256: f4d8f49574a4e396f34567f3eec4d38ab9c3910818dec22ca42b2a467c685d8b + url: "https://pub.dev" source: hosted - version: "2.7.1" + version: "3.12.1" firebase_core_platform_interface: dependency: transitive description: name: firebase_core_platform_interface - url: "https://pub.dartlang.org" + sha256: d7253d255ff10f85cfd2adaba9ac17bae878fa3ba577462451163bd9f1d1f0bf + url: "https://pub.dev" source: hosted - version: "4.5.3" + version: "5.4.0" firebase_core_web: dependency: transitive description: name: firebase_core_web - url: "https://pub.dartlang.org" + sha256: faa5a76f6380a9b90b53bc3bdcb85bc7926a382e0709b9b5edac9f7746651493 + url: "https://pub.dev" source: hosted - version: "2.2.2" + version: "2.21.1" flutter: dependency: "direct main" description: flutter @@ -107,109 +120,155 @@ packages: description: flutter source: sdk version: "0.0.0" - js: + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" + url: "https://pub.dev" + source: hosted + version: "10.0.7" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" + url: "https://pub.dev" + source: hosted + version: "3.0.8" + leak_tracker_testing: dependency: transitive description: - name: js - url: "https://pub.dartlang.org" + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" source: hosted - version: "0.6.4" + version: "3.0.1" matcher: dependency: transitive description: name: matcher - url: "https://pub.dartlang.org" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb + url: "https://pub.dev" source: hosted - version: "0.12.12" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - url: "https://pub.dartlang.org" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec + url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - url: "https://pub.dartlang.org" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 + url: "https://pub.dev" source: hosted - version: "1.8.0" + version: "1.15.0" path: dependency: transitive description: name: path - url: "https://pub.dartlang.org" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" + url: "https://pub.dev" source: hosted - version: "1.8.2" + version: "1.9.0" plugin_platform_interface: dependency: transitive description: name: plugin_platform_interface - url: "https://pub.dartlang.org" + sha256: dbf0f707c78beedc9200146ad3cb0ab4d5da13c246336987be6940f026500d3a + url: "https://pub.dev" source: hosted version: "2.1.3" rxdart: dependency: "direct main" description: name: rxdart - url: "https://pub.dartlang.org" + sha256: "0c7c0cedd93788d996e33041ffecda924cc54389199cde4e6a34b440f50044cb" + url: "https://pub.dev" source: hosted version: "0.27.7" sky_engine: dependency: transitive description: flutter source: sdk - version: "0.0.99" + version: "0.0.0" source_span: dependency: transitive description: name: source_span - url: "https://pub.dartlang.org" + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - url: "https://pub.dartlang.org" + sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" + url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.12.0" stream_channel: dependency: transitive description: name: stream_channel - url: "https://pub.dartlang.org" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 + url: "https://pub.dev" source: hosted - version: "2.1.0" + version: "2.1.2" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.dartlang.org" + sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" + url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.3.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.dartlang.org" + sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 + url: "https://pub.dev" source: hosted version: "1.2.1" test_api: dependency: transitive description: name: test_api - url: "https://pub.dartlang.org" + sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" + url: "https://pub.dev" source: hosted - version: "0.4.13" + version: "0.7.3" vector_math: dependency: transitive description: name: vector_math - url: "https://pub.dartlang.org" + sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" + url: "https://pub.dev" source: hosted - version: "2.1.3" + version: "2.1.4" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b + url: "https://pub.dev" + source: hosted + version: "14.3.0" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.dev" + source: hosted + version: "1.1.1" sdks: - dart: ">=2.18.0-146.0.dev <3.0.0" - flutter: ">=1.20.0" + dart: ">=3.6.1 <4.0.0" + flutter: ">=3.22.0" diff --git a/pubspec.yaml b/pubspec.yaml index 3b3d4c0..232dbf2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,13 +4,13 @@ version: 2.3.15 homepage: https://github.com/beerstorm-net/geoflutterfire2 environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ^3.6.1 flutter: ">=1.17.0" dependencies: flutter: sdk: flutter - cloud_firestore: ^4.4.4 + cloud_firestore: ^5.6.5 rxdart: ^0.27.7 dev_dependencies: