Flutter - Firebase - Delete a document without having access to it - flutter

I am trying to have access to notificationId once it gets created however the delete function deletes all the documents under this collection ('user-notifications').
Do you know what I need to change so I can remove only one document rather than all documents in this collection?
Future<String> likeAnnouncementNotification(String announcementId,
String imageUrl, String ownerUid, String uid, List liked) async {
String notificationid = const Uuid().v1();
String res = "Some error occurred";
try {
if (liked.contains(uid)) {
FirebaseFirestore.instance
.collection('notifications')
.doc(ownerUid)
.collection('user-notifications')
.where("uid", isEqualTo: FirebaseAuth.instance.currentUser?.uid)
.get()
.then((value) {
value.docs.forEach((document) {
document.reference.delete();
});
});
} else {
FirebaseFirestore.instance
.collection('notifications')
.doc(ownerUid)
.collection('user-notifications')
.doc(notificationid)
.set(
{
'imageUrl': imageUrl,
'announcementId': announcementId,
'notificationid': notificationid,
'timestamp': DateTime.now(),
'type': 0,
'uid': uid
},
);
}
res = 'success';
} catch (err) {
res = err.toString();
}
return res;
}

the only thing i see that you need to specify what notification document you went to delete add it like parameter when you call likeAnnouncementNotification function
Future<String> likeAnnouncementNotification(
String announcementId,
String imageUrl,
String ownerUid,
String uid,
List liked,
) async {
String notificationid = const Uuid().v1();
String res = "Some error occurred";
try {
if (liked.contains(uid)) {
FirebaseFirestore.instance
.collection('notifications')
.doc(ownerUid)
.collection('user-notifications')
.where("uid", isEqualTo: FirebaseAuth.instance.currentUser?.uid)
.get()
.then((value) {
value.docs.forEach((notification) {
FirebaseFirestore.instance
.collection('notifications')
.doc(ownerUid)
.collection('user-notifications')
.doc(notification.id) // this is the problem you need to specify what notification document you went to delete.
.delete();
});
});
} else {
FirebaseFirestore.instance
.collection('notifications')
.doc(ownerUid)
.collection('user-notifications')
.doc(notificationid)
.set(
{
'imageUrl': imageUrl,
'announcementId': announcementId,
'notificationid': notificationid,
'timestamp': DateTime.now(),
'type': 0,
'uid': uid
},
);
}
res = 'success';
} catch (err) {
res = err.toString();
}
return res;
}

Related

In Flutter How to add user id in firebase

how to put the user id in this code when I am generating a to-do list and i need to retrieve the tasks that a certain user?
Future<void> create(String todo, String description) async {
try {
await firestore.collection("Todo").add({
'todo': todo,
'description': description,
'timestamp': FieldValue.serverTimestamp()
});
} catch (e) {
print(e);
}
}
i solved my issue
Future<void> create(String todo, String description) async {
String? Uid= FirebaseAuth.instance.currentUser!.uid;
print(Uid);
print(FirebaseAuth.instance.currentUser!.uid);
try {
await firestore
.collection("TodoList")
.doc(Uid)
.collection("Todo")
.add({
'todo': todo,
'description': description,
'timestamp': FieldValue.serverTimestamp()
});
} catch (e) {
print(e);
}
}
Future<void> signUpController(email, password, username, phone) async {
print("$email,$password,$username,$phone");
try {
CommanDialog.showLoading();
final credential =
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email.trim(),
password: password,
);
print(credential);
CommanDialog.hideLoading();
try {
CommanDialog.showLoading();
var response =
await FirebaseFirestore.instance.collection('userlist').add({
'user_id': credential.user!.uid,
'user_name': username,
'phone': phone,
'password': password,
'joindate': DateTime.now().millisecondsSinceEpoch,
'email': email
});
print("response:: ${response.toString()}");
CommanDialog.hideLoading();
Get.back();
} catch (execption) {
CommanDialog.hideLoading();
print("error saving data ${execption}");
}
Get.back();
} on FirebaseAuthException catch (e) {
CommanDialog.hideLoading();
if (e.code == 'weak-password') {
CommanDialog.showErrorDialog(
description: "The password provided is too weak.");
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
CommanDialog.showErrorDialog(
description: "The account already exists for that email.");
print('The account already exists for that email.');
}
} catch (e) {
CommanDialog.hideLoading();
CommanDialog.showErrorDialog(description: "something went wrong");
print(e);
}
}

While creating user getting user as null

(Flutter) User is always getting null and unable to create user in realtime database
Future validateform() async {
FormState? formState = _formKey.currentState;
if(formState!.validate()) {
formState.reset();
User? user = firebaseAuth.currentUser;
// String? useruid = user!.uid;
print(user);
if(user == null ) {
print(_emailTextController.text);
print(_nameTextController.text);
print('UserID: '+'{$user}');
print(gender);
firebaseAuth
.createUserWithEmailAndPassword(
email: _emailTextController.text,
password: _passwordTextController.text)
.then((user) => {
_userServices.createUser(
{
"username": _nameTextController.text,
"email": _emailTextController.text,
// "userId": useruid,
"userId": user.user!.uid,
// "userId": "5as564d65as4d65as4d65as4d64",
"gender": gender,
})
}).catchError((err) => {print(err.toString())});
// });
print(user);
if (user!=null) {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => HomePage()),
);
}
}
}
}
I was expecting it to creaete in REALTIME DATABASE as user details, but user is always null.

Unable to store data in cloud firestore

I am unable to store data in cloud firestore. Authentication is all correct but the data is not going in cloud firestore and because of that login also not working because cloud firestore is pre-req to login function
That's my AuthContoller function
storeUserData({name, password, email}) async {
DocumentReference store = firestore
.collection("users")
.doc(FirebaseAuth.instance.currentUser!.uid);
store.set(
{'name': name, 'password': password, 'email': email, 'imageUrl': ''});
}
and that's my signup button code
try {
await controller
.signupMethod(
context: context,
email: emailController.text,
password: passwordController.text,
)
.then((value) {
** return controller
.storeUserData(
email: emailController.text,
name: nameController.text,
password: passwordController.text)
.then((value) {
VxToast.show(context,
msg: 'Account Created Sucessfully');
Get.offAll(LoginScreen());
}); **
});
} catch (e) {
FirebaseAuth.instance.signOut();
VxToast.show(context, msg: e.toString());
}
try this
storeUserData({name, password, email}) async {
final _firestore = FirebaseFirestore.instance;
await _firestore.collection("users")
.doc(FirebaseAuth.instance.currentUser!.uid).set(
{'name': name, 'password': password, 'email': email, 'imageUrl': ''});
}

No object exists at the desired reference

I am trying to get an image from the storage and use the url for a default profile picture but i am getting this error.
firebase_storage/object-not-found No object exists at the desired reference.
This is my code.
void authenticateStudent() async {
User? currentStudent;
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
await firebaseAuth
.createUserWithEmailAndPassword(
email: umailController.text.trim(),
password: passwordController.text.trim(),
)
.then((auth) {
currentStudent = auth.user;
}).catchError((onError) {
print(onError);
});
if (currentStudent != null) {
FirebaseStorage.instance
.ref()
.child('profile')
.getDownloadURL()
.then((url) {
imageUrl = url;
});
saveDataToFirestore(currentStudent!).then((value) {
Navigator.pop(context);
print("User added successfully");
});
}
}
Future saveDataToFirestore(User currentStudent) async {
FirebaseFirestore.instance
.collection("students")
.doc(currentStudent.uid)
.set({
"studentUID": currentStudent.uid,
"fname": fnameController.text.trim(),
"lname": lnameController.text.trim(),
"Mobile": phoneController.text.trim(),
"Program": selectedProgram,
"student_id": studentidController.text.trim(),
"cohort": selectedCohort,
"umail": currentStudent.email,
"profilepicture": imageUrl,
"active": active,
"status": status
});
}
The database is structured like this
This is my code.
void authenticateStudent() async {
User? currentStudent;
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
await firebaseAuth
.createUserWithEmailAndPassword(
email: umailController.text.trim(),
password: passwordController.text.trim(),
)
.then((auth) {
currentStudent = auth.user;
}).catchError((onError) {
print(onError);
});
if (currentStudent != null) {
FirebaseStorage.instance
.ref()
.child('profile')
.getDownloadURL()
.then((url) {
imageUrl = url;
});
saveDataToFirestore(currentStudent!).then((value) {
Navigator.pop(context);
print("User added successfully");
});
}
}
Future saveDataToFirestore(User currentStudent) async {
FirebaseFirestore.instance
.collection("students")
.doc(currentStudent.uid)
.set({
"studentUID": currentStudent.uid,
"fname": fnameController.text.trim(),
"lname": lnameController.text.trim(),
"Mobile": phoneController.text.trim(),
"Program": selectedProgram,
"student_id": studentidController.text.trim(),
"cohort": selectedCohort,
"umail": currentStudent.email,
"profilepicture": imageUrl,
"active": active,
"status": status
});
}
The database is structured like this
Your code says:
FirebaseStorage.instance
.ref()
.child('profile')
.getDownloadURL()
But in the screenshot, the file is called profile.png. The path must match completely and exactly, so:
FirebaseStorage.instance
.ref()
.child('profile.png')
.getDownloadURL()

The client_secret Provided does not match any associated PaymentIntent on this account

I'm trying to use flutter_stripe for a stripe connect account, But I always get the
same error: The client_secret provided doesn't match the client_secret associated with the PaymentIntend.
I've completed all steps according to flutter_stripe but I still face this error.
Below is my code Please check this and help me.
inde.js
const functions = require("firebase-functions");
const stripe = require("stripe")("secret_key");
exports.stripePaymentIntentRequest = functions.https.onRequest(async (req, res) => {
try {
let customerId;
//Gets the customer who's email id matches the one sent by the client
const customerList = await stripe.customers.list({
email: req.body.email,
limit: 1
});
//Checks the if the customer exists, if not creates a new customer
if (customerList.data.length !== 0) {
customerId = customerList.data[0].id;
}
else {
const customer = await stripe.customers.create({
email: req.body.email
});
customerId = customer.data.id;
}
//Creates a temporary secret key linked with the customer
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customerId },
{ apiVersion: '2020-08-27' }
);
//Creates a new payment intent with amount passed in from the client
const paymentIntent = await stripe.paymentIntents.create({
amount: parseInt(req.body.amount),
currency: 'usd',
customer: customerId,
})
res.status(200).send({
clientSecret: paymentIntent.client_secret,
paymentIntent: paymentIntent,
ephemeralKey: ephemeralKey.secret,
customer: customerId,
success: true,
})
} catch (error) {
res.status(404).send({ success: false, error: error.message })
}
});
PaymentService.dart
Future<void> initPaymentSheet(
{required BuildContext context, required String email, required int amount}) async {
try {
// 1. create payment intent on the server
final response = await http.post(
Uri.parse(
'Firebase api link of Functions'),
body: {
'email': email,
'amount': amount.toString(),
});
Map<String, dynamic> paymentIntentBody = jsonDecode(response.body);
log(paymentIntentBody.toString());
//2. initialize the payment sheet
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntentBody["clientSecret"],
merchantDisplayName: 'Flutter Stripe Store Demo',
customerId: paymentIntentBody['customer'],
customerEphemeralKeySecret: paymentIntentBody['ephemeralKey'],
style: ThemeMode.light,
testEnv: true,
merchantCountryCode: 'US',
),
);
await Stripe.instance.presentPaymentSheet();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Payment completed!')),
);
} catch (e) {
if (e is StripeException) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error from Stripe: ${e.error.localizedMessage}'),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error the Stripe of : $e')),
);
}
}
}
The log error print on my console is :
> [log] {paymentIntent:
> pi_3LI2acCTAUDjRNFV1Ra3dahz_secret_Fcqw73pWrE4avKRyuDVzRBitG,
> ephemeralKey:
> ek_test_YWNjdF8xSlQ3amtDVEFVRGpSTkZWLDl1OE5Vdm1jTGY4T1RpaVhHOTB3NTRVSkQ5UGl4azA_00j32OYG9n,
> customer: cus_LHG2YpQP9Cgwuy, success: true}
The following code is from a previous Stripe evaluation stage. But it worked. Slim it down to your needs.
Remember to publish your secret key to the server, so the server can talk to Stripe.
code.dart
Future<bool> payWithPaymentSheet(
ProductModel productModel, PriceModel priceModel,
{String merchantCountryCode = 'DE'}) async {
if (kIsWeb) {
throw 'Implementation not availabe on Flutter-WEB!';
}
String uid = AuthService.instance.currentUser().uid;
String email = AuthService.instance.currentUser().email ?? '';
HttpsCallableResult response;
try {
response = await FirebaseFunctions
.httpsCallable('createPaymentIntent')
.call(<String, dynamic>{
'amount': priceModel.unitAmount,
'currency': priceModel.currency,
'receipt_email': email,
'metadata': {
'product_id': productModel.id,
'user_id': uid,
"valid_until": productModel.getUntilDateTime().toIso8601String(),
'product_name': productModel.name.tr,
},
'testEnv': kDebugMode,
});
} on FirebaseFunctionsException catch (error) {
log(error.code);
log(error.details);
log(error.message ?? '(no message)');
Get.snackbar(
error.code,
error.message ?? '(no message)',
icon: const Icon(Icons.error_outline),
);
return false;
}
Map<String, dynamic> paymentIntentBody = response.data;
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntentBody["clientSecret"],
currencyCode: priceModel.currency,
applePay: false,
googlePay: false,
merchantCountryCode: merchantCountryCode,
merchantDisplayName: Strings.appName,
testEnv: kDebugMode,
customerId: paymentIntentBody['customer'],
customerEphemeralKeySecret: paymentIntentBody['ephemeralKey'],
));
try {
await Stripe.instance.presentPaymentSheet();
return true;
} on StripeException catch (e) {
log(e.error.code.name);
log(e.error.message ?? '(no message)');
log(e.error.localizedMessage ?? '(no message)');
Get.snackbar(e.error.code.name, e.error.message ?? '',
icon: const Icon(Icons.error_outline));
} catch (e) {
Get.snackbar('An unforseen error occured', e.toString(),
icon: const Icon(Icons.error_outline));
}
return false;
}
index.ts
// SETTING SECRET KEY ON SERVER:
// cd functions
// firebase functions:config:set stripe.secret_key="sk_live_51L...Noe"
// firebase deploy --only functions
let stripe = require("stripe")(functions.config().stripe.secret_key);
exports.createPaymentIntent = functions
.https.onCall((data, context) => {
// if (!context.auth) {
// return { "access": false };
// }
return new Promise(function (resolve, reject) {
stripe.paymentIntents.create({
amount: data.amount,
currency: data.currency,
receipt_email: decodeURIComponent(data.receipt_email),
metadata: data.metadata,
}, function (err, paymentIntent) {
if (err != null) {
functions.logger.error("Error paymentIntent: ", err);
reject(err);
}
else {
resolve({
clientSecret: paymentIntent.client_secret,
paymentIntentData: paymentIntent,
});
}
});
});
});