螢屏截圖 如何解決問題 錯誤:狀態不佳:無法在不存在的 DocumentSnapshotPlatform 上獲取欄位?
依賴:
√ 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.1: firebase_storage: ^8.1.3 √chips_choice: ^2.0.1 √ flutter_switch: ^0.2.1 √ email_validator: ^1.0.6
我的 GitHub 倉庫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();
},
),
],
);
},
);
}
}
uj5u.com熱心網友回復:
好吧,當您想在云 Firestore 中獲取或設定一個不存在的欄位時,通常會發生這種情況,在這種情況下,這可能是您輸入的錯字,因此請交叉檢查或單擊控制臺上的錯誤訊息并發送螢屏截圖導致該錯誤的特定代碼行
uj5u.com熱心網友回復:
這是用于檢查用戶是否登錄的代碼行:
_services.getAdminCredentials(username)
并且 getAdminCredentials 是這樣定義的;
Future<DocumentSnapshot> getAdminCredentials(id) {
var result = FirebaseFirestore.instance.collection('Admin').doc(id).get();
return result;
}
因此,如果您呼叫_services.getAdminCredentials('Admin'),您希望獲得 id = 'Admin' 的檔案。從您的 Firestore 螢屏截圖中,您的檔案 ID 是“qysr...”。所以它不會作業。相反,像這樣重寫你的代碼:
Future<QuerySnapshot<Map<String, dynamic>>> getAdminCredentials(String id) {
var result = FirebaseFirestore.instance
.collection('Admin')
.where('username', isEqualTo: id)
.get();
return result;
}
然后在你的 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.
或者更好的是,您可以使用 id 作為用戶名保存您的檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/343520.html
上一篇:Flutter:持久化資料
