File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import 'package:firebase_auth/firebase_auth.dart' ;
2
+ import 'package:cloud_firestore/cloud_firestore.dart' ;
3
+
4
+ FirebaseAuth auth = FirebaseAuth .instance;
5
+ CollectionReference usersCollection =
6
+ FirebaseFirestore .instance.collection ('users' );
7
+ CollectionReference congestionCollection =
8
+ FirebaseFirestore .instance.collection ('congestions' );
9
+ bool newUser = true ;
10
+
11
+ class FirebaseCalls {
12
+ Future <void > updateUser (String username, List <Map <String , dynamic >> addresses) async {
13
+ // Fetch the currently authenticated user
14
+ User ? currentUser = auth.currentUser;
15
+
16
+ // Ensure that the user is logged in
17
+ if (currentUser == null ) {
18
+ throw Exception ("No user is logged in." );
19
+ }
20
+
21
+ // Check if there is an existing record of user
22
+ QuerySnapshot querySnap = await usersCollection.where ('userid' , isEqualTo: currentUser.uid).get ();
23
+
24
+ if (querySnap.docs.isNotEmpty) {
25
+ // Existing user
26
+ QueryDocumentSnapshot doc = querySnap.docs[0 ];
27
+ await doc.reference.update ({
28
+ 'addresses' : addresses,
29
+ });
30
+ } else {
31
+ // New user
32
+ await usersCollection.add ({
33
+ 'addresses' : addresses,
34
+ 'username' : username,
35
+ 'userid' : currentUser.uid,
36
+ });
37
+ }
38
+ }
39
+ }
40
+
You can’t perform that action at this time.
0 commit comments