Screenshot How to solve the problem Error: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist ?
dependencies:
√ firebase_core: ^1.3.0 √ cloud_firestore: ^2.2.2 √ ars_dialog: ^1.0.8 √ alert_dialog: ^1.0.0 √ firebase_auth: ^1.4.1 √ flutter_admin_scaffold: ^0.0.5 √ firebase: ^9.0.1 √ firebase_storage: ^8.1.3 √ chips_choice: ^2.0.1 √ flutter_switch: ^0.2.1 √ email_validator: ^1.0.6
My GitHub repo https://github.com/arafat1971/jaitunapp_web-master
class FirebaseServices {
FirebaseFirestore firestore = FirebaseFirestore.instance;
CollectionReference banners = FirebaseFirestore.instance.collection('slider');
CollectionReference vendors =
FirebaseFirestore.instance.collection('vendors');
CollectionReference category =
FirebaseFirestore.instance.collection('category');
CollectionReference boys = FirebaseFirestore.instance.collection('boys');
FirebaseStorage storage = FirebaseStorage.instance;
Future<DocumentSnapshot> getAdminCredentials(id) {
var result = FirebaseFirestore.instance.collection('Admin').doc(id).get();
return result;
}
//Banner
Future<String> uploadBannerImageToDb(url) async {
String downloadUrl = await storage.ref(url).getDownloadURL();
if (downloadUrl != null) {
firestore.collection('slider').add({
'image': downloadUrl,
});
}
return downloadUrl;
}
deleteBannerImageFromDb(id) async {
firestore.collection('slider').doc(id).delete();
}
//vendor
updateVendorStatus({id, status}) async {
vendors.doc(id).update({'accVerified': status ? false : true});
}
updateTopPickedVendor({id, status}) async {
vendors.doc(id).update({'isTopPicked': status ? false : true});
}
Future<String> uploadCategoryImageToDb(url, catName) async {
String downloadUrl = await storage.ref(url).getDownloadURL();
if (downloadUrl != null) {
category.doc(catName).set({
'image': downloadUrl,
'name': catName,
});
}
return downloadUrl;
}
Future<void> saveDeliverBoys(email, password) async {
boys.doc(email).set({
'accVerified': false,
'address': '',
'email': email,
'imageUrl': '',
'location': GeoPoint(0, 0),
'mobile': '',
'name': '',
'password': password,
'uid': ''
});
}
//update delivery boy approved status
updateBoyStatus({id, context, status}) {
ProgressDialog progressDialog = ProgressDialog(context,
blur: 2,
backgroundColor: Color(0xFF84c225).withOpacity(.3),
transitionDuration: Duration(milliseconds: 500));
progressDialog.show();
// Create a reference to the document the transaction will use
DocumentReference documentReference =
FirebaseFirestore.instance.collection('boys').doc(id);
return FirebaseFirestore.instance.runTransaction((transaction) async {
// Get the document
DocumentSnapshot snapshot = await transaction.get(documentReference);
if (!snapshot.exists) {
throw Exception("User does not exist!");
}
// Update the follower count based on the current count
// Note: this could be done without a transaction
// by updating the population using FieldValue.increment()
// Perform an update on the document
transaction.update(documentReference, {'accVerified': status});
}).then((value) {
progressDialog.dismiss();
showMyDialog(
title: 'Delivery Boy Status',
message: status == true
? "Delivery boy approved status updated as Approved"
: "Delivery boy approved status updated as Not Approved",
context: context);
}).catchError((error) => showMyDialog(
context: context,
title: 'Delivery Boy Status',
message: "Failed to update delivery boy status: $error",
));
}
Future<void> confirmDeleteDialog({title, message, context, id}) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(message),
],
),
),
actions: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Delete'),
onPressed: () {
deleteBannerImageFromDb(id);
Navigator.of(context).pop();
},
),
],
);
},
);
}
Future<void> showMyDialog({title, message, context}) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(message),
],
),
),
actions: <Widget>[
TextButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
CodePudding user response:
Well this usually happens when you want to get or set a field in cloud Firestore that doesn't exist in this case it might be a typo that you made so Cross check or rather click on the error message on the console and send a screenshot of that particular line of code causing that error
CodePudding user response:
This is the line of code you use to check if a user is logged in:
_services.getAdminCredentials(username)
and getAdminCredentials is defined like this;
Future<DocumentSnapshot> getAdminCredentials(id) {
var result = FirebaseFirestore.instance.collection('Admin').doc(id).get();
return result;
}
So if you call _services.getAdminCredentials('Admin')
, you want to get the document with id = 'Admin'. From your Firestore screenshot, your document id is 'qysr...'. So it won't work. Rather, rewrite your code like this:
Future<QuerySnapshot<Map<String, dynamic>>> getAdminCredentials(String id) {
var result = FirebaseFirestore.instance
.collection('Admin')
.where('username', isEqualTo: id)
.get();
return result;
}
Then call it like this in your login.dart
_services.getAdminCredentials(username).then((value) async {
if (value.docs.isNotEmpty) {
// document exist
if (value.docs.first.get('username') == username) {
// username is correct
if (value.docs.first.get('username') == password) {
// password is correct
// continue the rest of your code.
OR better still, you can save your documents using id as username