错误:状态错误:无法在不存在的DocumentSnapshotPlatform上获取字段 [英] Error: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

查看:0
本文介绍了错误:状态错误:无法在不存在的DocumentSnapshotPlatform上获取字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Screenshot如何解决问题错误:状态不正确:无法在不存在的DocumentSnapshotPlatform上获取字段?

依赖项:

√FireBASE_CORE:^1.3.0 √CLOUD_FIREST:^2.2.2 √ARS_DIALOG:^1.0.8 √警报对话框:^1.0.0 √Firebase身份验证:^1.4.1 √Ffltter_ADMIN_SCADFFORD:^0.0.5 √FireBase:^9.0.1 √FireBASE_STORAGE:^8.1.3 √CHIPS_CHOICE:^2.0.1 √Ffltter_Switch:^0.2.1 √电子邮件验证器:^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();
              },
            ),
          ],
        );
      },
    );
  }
}

推荐答案

以下代码行用于检查用户是否已登录: _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;
}

然后在您的登录中这样称呼它。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作为用户名保存您的文档

这篇关于错误:状态错误:无法在不存在的DocumentSnapshotPlatform上获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆