Advertisement
anik11556

updating and newfield to the firebase

Nov 8th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. floatingActionButton: FloatingActionButton(
  2. onPressed: () async {
  3.  
  4.  
  5. List<TempPost> posts = await fetchallPost() ;
  6. print("return back to floating action");
  7.  
  8.  
  9.  
  10. /*
  11. posts.forEach((element) {
  12. print(element.description);
  13. print(element.ownerPhotoUrl);
  14. print(element.id);
  15. print(element.ownerEmail);
  16. print(element.ownerId);
  17. print(".................................................");
  18. });
  19.  
  20. */
  21.  
  22. /*
  23. flutter ( 3738): return back to floating action
  24. I/flutter ( 3738): Image tells the secret
  25. I/flutter ( 3738): https://firebasestorage.googleapis.com/v0/b/socialmdeiaapp.appspot.com/o/images%2F1685724234769?alt=media&token=b2f67a4e-8587-4d9b-aa31-51fc035ac3d4
  26. I/flutter ( 3738): 1lNW54eZ87CJpMSY2oO0
  27. I/flutter ( 3738): anik11556@gmail.com
  28. I/flutter ( 3738): YKjcFlzKIrf4kSVxHWQJEcqIutZ2
  29. */
  30. String? newPostId; //= "1lNW54eZ87CJpMSY2oO0";
  31. String? fieldvalue; //= "anik11556@gmail.com";
  32. CollectionReference collection = FirebaseFirestore.instance.collection('all_Posts');
  33.  
  34.  
  35. /*
  36.  
  37.  
  38. try {
  39. await collection.doc(newPostId).update({
  40. 'ownerEmail': fieldvalue,
  41. });
  42. print('Field "newField" added successfully.');
  43. } catch (e) {
  44. print('Error adding field: $e');
  45. }
  46.  
  47.  
  48. */
  49.  
  50.  
  51.  
  52. posts.forEach((element) async {
  53. newPostId = element.id;
  54. fieldvalue = element.ownerPhotoUrl;
  55. try {
  56. await collection.doc(newPostId).update({
  57. 'ownerPhotoUrl': fieldvalue,
  58. });
  59. print('Field "postId" updated successfully.');
  60. } catch (e) {
  61. print('Error updating field: $e');
  62. }
  63. });
  64.  
  65.  
  66.  
  67.  
  68. },
  69. ),
  70. //////////////////////////////////////////////////////////////////////////////////////////
  71.  
  72.  
  73. List<TempPost> tempPosts = [];
  74.  
  75. Future<List<TempPost>> fetchallPost() async {
  76.  
  77. print("user fetching");
  78. List<User> users = await GetWant().getAllUserfromJson();
  79. //users.forEach((element) {print(element.userName);});
  80.  
  81. print("Post Fetching");
  82. CollectionReference collection = FirebaseFirestore.instance.collection('all_Posts');
  83. QuerySnapshot querySnapshot = await collection.get();
  84.  
  85.  
  86. querySnapshot.docs.forEach((doc) {
  87.  
  88. String? idd = doc.id;
  89. String? id = doc.id;//doc.get('id');
  90. String? postId = doc.get('postId');
  91. String? userName = doc.get('userName');
  92.  
  93.  
  94. String? ownerId = doc.get('ownerId');
  95.  
  96.  
  97. String? location = doc.get('location');
  98. String? timestamp = doc.get('timestamp');
  99. String? mediaUrl = doc.get('mediaUrl');
  100. String? description = doc.get('description');
  101.  
  102.  
  103. /*
  104. Post curPost = Post.Complete(id,postId,userName,ownerId,location,timestamp,mediaUrl,description);
  105. bool exists = Post.posts.any((entity) => entity.postId == curPost.postId);
  106. */
  107.  
  108.  
  109. /*
  110. print(idd);
  111. print(postId);
  112. */
  113.  
  114. int index = users.indexWhere((obj) => obj.id == ownerId);
  115. /*
  116. print(ownerId);
  117. print(users[index].email);
  118. print(users[index].userName);
  119. print(users[index].id);
  120. */
  121.  
  122.  
  123. ownerId = users[index].id ;
  124. String? ownerEmail=users[index].email;
  125. String? ownerPhotoUrl=users[index].photoUrl;
  126.  
  127.  
  128. TempPost curPost = TempPost.Complete(id,postId,userName,ownerId,location,timestamp,mediaUrl,description,ownerEmail,ownerPhotoUrl);
  129. bool exists = tempPosts.any((entity) => entity.postId == curPost.postId);
  130.  
  131.  
  132.  
  133.  
  134.  
  135. // print(curUser.userName);
  136. // //print(DateFormat("MMMM d, y 'at' h:mm:ss a 'UTC'z").format(curUser.lastSeen.toDate()).toString());
  137. // print(curUser.time.toDate());
  138. if(exists)
  139. {
  140. //print("already in the list");
  141. }
  142. else
  143. {
  144. // print("not exist in the list");
  145. tempPosts.add(curPost);
  146. }
  147.  
  148. });
  149.  
  150. print(tempPosts.length);
  151.  
  152. return tempPosts;
  153.  
  154. }
  155.  
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement