Advertisement
jevixlugya

trying to post with a user name

Apr 12th, 2024
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.01 KB | None | 0 0
  1.   //get collection posts
  2.   final CollectionReference posts = FirebaseFirestore.instance.collection("User posts");
  3.   Future<void> getUsername() async {
  4.     try {
  5.       final DocumentSnapshot docRef = await FirebaseFirestore.instance
  6.           .collection("Users")
  7.           .doc("${user!.email}").get();
  8.  
  9.       if (docRef.exists) {
  10.         final map = docRef.data() as Map<String, dynamic>;
  11.         String name = map['username'];
  12.         print(name); //or add your own logic to handle the user's info
  13.       } else {
  14.         print("document with id : \"${user!.email}\" does not exists");
  15.       }
  16.  
  17.     } catch (e) {
  18.       print(e.toString());
  19.       rethrow;
  20.     }
  21.   }
  22.   //post the [posts]
  23.  
  24.   Future<void> addPosts(String message, String title, fileurl)  {
  25.  
  26.     final data = getUsername();
  27.     return posts.add({
  28.     'posttitle': title,
  29.     'UserEmail': user!.email,
  30.     'postMessage': message,
  31.     'TimeStamp': Timestamp.now(),
  32.     'Likes': [],
  33.     'comments': [],
  34.     'postfile': fileurl,
  35.     });
  36.  
  37.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement