0
votes

what i am trying to do is query a specific document using the 'Document ID'. however the query comes up null. here is my code.

 void getEquipDetail() async {
    await _firestore
        .collection('users')
        .document(_userUid)
        .collection('equipment')
        .document(documentID)
        .get()
        .then((DocumentSnapshot ds) {
      equipName = ds.data['equip_name'];
      print(equipName);
    });
  }

this works fine when i had 'equipment' as a root-level collection, but since making it a subcollection of 'users' i cannot query a specific document.

here is the db structure currently -collection (users) * root level -document (userid) -collection(equipment) -document (equipment documents) * this is what im trying to query

here is the error being returned

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("equip_name")
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _EquipmentDetailScreenState.getEquipDetail.<anonymous closure> (package:simmanager/screens/equip_detail_screen.dart:52:26)
#2      _rootRunUnary (dart:async/zone.dart:1132:38)
#3      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#4      _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#5      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#6      Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#7      Future._completeWithValue (dart:async/future_impl.dart:522:5)
#8      _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#9      _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#10     DocumentReference.get (package:cloud_firestore/src/document_reference.da<…>

this is the code that works without any issue (its for items instead of equipment)

void getItemDetail() async {
    await Firestore.instance
        .collection('items')
        .document(widget.item.document)
        .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'];
1
What's the problem? What exactly isn't working the way you expect? Your code doesn't seem to be checking if the document actually exists. Maybe it doesn't.Doug Stevenson
the issue is that this is returning a null. i have verified the document exists (i check and print the userid and document id prior to running this function. im only running into this issue when querying a document nested under 2 collections.David Escobar
What exactly is returning null? Please be specific about what you're observing. Please also share code that does not "return null" by whatever observation you're making.Doug Stevenson

1 Answers

0
votes

Your code in the first example doesn't really match the code in the second example. Please compare them carefully. For the first example, it looks like you meant to say:

equipName = ds['equip_name'];