From 7b379914e44583b9b097ed286b669ad244b176a1 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Thu, 21 Dec 2023 01:26:51 +0200 Subject: Added Firestore support for both the driver app and the rider mobile app --- mobile/lib/routes.dart | 68 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 17 deletions(-) (limited to 'mobile/lib/routes.dart') 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 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> getDataFromFirestore() async { + List 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 { + List routes = []; + + @override + void initState() { + super.initState(); + fetchRoutesFromFirestore(); + } + + Future fetchRoutesFromFirestore() async { + List 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(); -- cgit v1.2.3