diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-12-21 01:26:51 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-12-21 01:26:51 +0200 |
| commit | 7b379914e44583b9b097ed286b669ad244b176a1 (patch) | |
| tree | 32fc660273161a92a0b9784ac5edd6f2f5eea58c /mobile/lib/routes.dart | |
| parent | b84fe9a9c4c18a3f4e957f76ead34403c4316f76 (diff) | |
| download | carpool-7b379914e44583b9b097ed286b669ad244b176a1.tar.xz carpool-7b379914e44583b9b097ed286b669ad244b176a1.zip | |
Added Firestore support for both the driver app and the rider mobile app
Diffstat (limited to 'mobile/lib/routes.dart')
| -rw-r--r-- | mobile/lib/routes.dart | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/mobile/lib/routes.dart b/mobile/lib/routes.dart index de5864c..5073271 100644 --- a/mobile/lib/routes.dart +++ b/mobile/lib/routes.dart @@ -1,7 +1,11 @@ import 'package:flutter/material.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; import 'cart.dart'; import 'package:intl/intl.dart'; +// Accessing Firestore instance +final FirebaseFirestore firestore = FirebaseFirestore.instance; + class Route { final String name; final String startLocation; @@ -13,30 +17,60 @@ class Route { required this.endLocation}); } -class RoutesPage extends StatelessWidget { - final List<Route> dummyRoutes = [ - Route( - name: 'Morning Ride - Gate 3 to Abdu-Basha', - startLocation: 'Abassyia', - endLocation: 'Abdu-Basha Gate-3'), - Route( - name: 'Morning Ride - Abdu-Basha to 5th Settlement', - startLocation: 'Abdu-Basha', - endLocation: '5th Settlement'), - Route( - name: 'Afternoon Ride - Abdu-Basha to Gate 3', - startLocation: 'Hadayek Elkoba', - endLocation: 'Abdu-Basha Gate-6'), - ]; +Future<List<Route>> getDataFromFirestore() async { + List<Route> routes = []; + try { + // Accessing a specific collection ('users' in this case) + QuerySnapshot querySnapshot = await firestore.collection('Rides').get(); + + // Loop through the documents in the collection + querySnapshot.docs.forEach((doc) { + String name = doc['driverName']; + // String carModel = doc['carModel']; + // String carColor = doc['carColor']; + // String plateNumber = doc['plateNumber']; + // String status = doc['status']; + // DateTime orderTime = doc['orderTime']; + String fromLocation = doc['fromLocation']; + String toLocation = doc['toLocation']; + routes.add(Route( + name: name, startLocation: fromLocation, endLocation: toLocation)); + }); + } catch (e) { + print('Error retrieving data: $e'); + } + return routes; +} + +class RoutesPage extends StatefulWidget { + @override + _RoutesPageState createState() => _RoutesPageState(); +} + +class _RoutesPageState extends State<RoutesPage> { + List<Route> routes = []; + + @override + void initState() { + super.initState(); + fetchRoutesFromFirestore(); + } + + Future<void> fetchRoutesFromFirestore() async { + List<Route> fetchedRoutes = await getDataFromFirestore(); + setState(() { + routes = fetchedRoutes; + }); + } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Routes')), body: ListView.builder( - itemCount: dummyRoutes.length, + itemCount: routes.length, itemBuilder: (BuildContext context, int index) { - final Route route = dummyRoutes[index]; + final Route route = routes[index]; return GestureDetector( onTap: () { DateTime now = DateTime.now(); |
