【问题标题】:Cannot query a document from a sub collection from Firestore collection无法从 Firestore 集合的子集合中查询文档
【发布时间】:2019-11-12 04:58:22
【问题描述】:

我正在尝试查询嵌套在 collection'users/document'useruid'/collection'items'/'document'documentid' 下的特定文档。我不断得到一个“空”值,它出错了:

   try {
      final user = await _auth.currentUser();
      if (user != null) {
        _loggedInUser = user;
        _userUid = _loggedInUser.uid;
        setState(() {
          // showSpinner = false;
          print(_userUid);
        });
      }
    } catch (e) {
      print(e);
    }
  }

  void getItemDetail() async {
    await _firestore
        .collection('users')
        .document(_userUid)
        .collection('items')
        .document(documentID)
        .get()
        .then((DocumentSnapshot snapshot) {
      itemDetails = snapshot.data;
      itemName = snapshot['item_name'];
      itemNum = snapshot['item_num'.toString()];
      itemDesc = snapshot['item_desc'];
      itemLoc = snapshot['item_location'];
      itemQty = snapshot['item_qty'.toString()];
      itemUom = snapshot['item_uom'];
      itemMfr = snapshot['item_mfr'];
      itemStock = snapshot['out_of_stock'];
      lastEditDate = snapshot['edit_date'];
      createDate = snapshot['create_date'];
      imageURL = snapshot['image_url'];

      setState(() {
        dateCreated =
            new DateFormat.yMMMMd("en_US").format(createDate.toDate());
        itemDetails = snapshot.data;
        showSpinner = false;
        stockCheck();
        editDateCheck();
      });
    });
  }
 @override
  void initState() {
    testApp();
    getCurrentUser();
    getItemDetail();
    //   editDateCheck();
    super.initState();
  }
flutter error output:
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("item_name")
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:29:42)
#2      _ItemDetailScreenState.getItemDetail.<anonymous closure> (package:simmanager/screens/item_detail_screen.dart:77:26)
#3      _rootRunUnary (dart:async/zone.dart:1132:38)
#4      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#5      _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#7      Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#8      Future._completeWithValue (dart:async/future_impl.dart:522:5)
#9      _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#10     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#<…>

documentid 是从上一页的导航器传递过来的。我已正确打印 (userid, documentid" 所有值。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    你应该经常检查你的快照是否有任何数据或 null 之类的,

    if(snapshot!=null)
    

    那么你应该总是检查快照里面的数据是空还是不喜欢,

    if(snapshot.data!=null)
    

    另外,在您的情况下,您称您是空对象上的数据。

    我认为您需要将所有项目的 snapshot['item_name'] 替换为 snapshot.data['item_name']

    您错误地调用了导致问题的 snapshot["your_field"] 而不是 snapshot.data["your_field"]。

    此外,建议始终在解析来自网络的数据时检查 null 安全性。

    【讨论】:

    • 感谢杰,我做了这些更改,但我仍然遇到同样的错误。在我在“用户”下创建集合“项目”之前,这很好用,以前“项目”是一个单独的集合
    • 您能否在此处更新您的问题并提供更改和错误?
    • 添加了 if 语句来检查快照是否 != null。对我来说,快照返回 null,但我相信 firestore 文档的路径是正确的。没有其他代码更改
    • 如果你得到的快照为空,那么进一步检查和解析数据就没有意义了。首先确保您的查询是正确的,并且您正在获取一些数据。否则解析数据是没有意义的。
    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 2021-04-08
    • 2020-12-02
    • 2021-07-12
    • 2018-06-19
    • 2020-07-24
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多