Advertisement
Hitesh_jadhav

firebase_tut-flutterfire

Mar 15th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import 'dart:html';
  2.  
  3. import 'package:cloud_firestore/cloud_firestore.dart';
  4. import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:firebase_core/firebase_core.dart';
  6.  
  7. Future<bool> signin(String email, String password) async {
  8. try {
  9. await FirebaseAuth.instance
  10. .signInWithEmailAndPassword(email: email, password: password);
  11. return true;
  12. } catch (e) {
  13. print(e);
  14. return false;
  15. }
  16. }
  17.  
  18. Future<bool> register(String email, String password) async {
  19. try {
  20. await FirebaseAuth.instance
  21. .createUserWithEmailAndPassword(email: email, password: password);
  22. return true;
  23. } on FirebaseAuthException catch (e) {
  24. if (e.code == 'weak-password') {
  25. print('The password provided is too weak');
  26. } else if (e.code == 'email-already-in-use') {
  27. print('The account already exists for that email.');
  28. }
  29. return false;
  30. } catch (e) {
  31. print(e.toString());
  32. return false;
  33. }
  34. }
  35.  
  36. // Future<bool> addCoin(String id, String amount) async {
  37. // try {
  38. // String? uid = FirebaseAuth.instance.currentUser?.uid;
  39. // var value = double.parse(amount);
  40. // DocumentReference documentReference = FirebaseFirestore.instance
  41. // .collection('users')
  42. // .doc(uid)
  43. // .collection('Coins')
  44. // .doc(id);
  45. // FirebaseFirestore.instance.runTransaction((transaction) async {
  46. // DocumentSnapshot snapshot = await transaction.get(documentReference);
  47. // if (!snapshot.exists) {
  48. // documentReference.set({"Amount": value});
  49. // return true;
  50. // }
  51. // double newAmount = snapshot.data()!['Amount'] + value;
  52. // transaction.update(documentReference, {'Amount': newAmount});
  53. // return true;
  54. // });
  55. // } catch (e) {
  56. // return false;
  57. // }
  58. // }
  59.  
  60. Future<bool> addCoin(String id, String amount) async {
  61. try {
  62. String? uid = FirebaseAuth.instance.currentUser?.uid;
  63. var value = double.parse(amount);
  64. DocumentReference documentReference = FirebaseFirestore.instance
  65. .collection('Users')
  66. .doc(uid)
  67. .collection('Coins')
  68. .doc(id);
  69. FirebaseFirestore.instance.runTransaction((transaction) async {
  70. DocumentSnapshot snapshot = await transaction.get(documentReference);
  71. if (!snapshot.exists) {
  72. documentReference.set({'Amount': value});
  73. return true;
  74. }
  75. double newAmount = snapshot.docs.data['Amount'] + value;
  76. transaction.update(documentReference, {'Amount': newAmount});
  77. return true;
  78. });
  79. return true;
  80. } catch (e) {
  81. return false;
  82. }
  83. }
  84.  
  85. // Future<bool> addCoin(String id, String amount) async {
  86. // try {
  87. // String uid = FirebaseAuth.instance.currentUser!.uid;
  88. // var value = double.parse(amount);
  89. // DocumentReference documentReference = FirebaseFirestore.instance
  90. // .collection('users')
  91. // .doc(uid)
  92. // .collection('Coins')
  93. // .doc(id);
  94. // } catch (e) {
  95. // return false;
  96. // }
  97. // }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement