0
Future<String> addPlayersToTable() async {
    var userName = await getUserName();
    userName = userName.toString();
    String uid = getUid();

    var photoUrl = await getPhotoUrl();
    photoUrl = photoUrl.toString();

    await _firestore
        .collection('public_tables')
        .doc()
        .collection('players')
        .doc(uid)
        .set({
      'username': userName,
      'photoUrl': photoUrl,
      'uid': uid,
      'currentPlayer': false
    });

    var count = await countTablePlayers();

    print(count);

    return count;
  }

  Future countTablePlayers() async {
    var allDocs = await _firestore.collection('public_tables').get();
    var docId = allDocs.docs.last.id;

    var count = await _firestore
        .collection('public_tables')
        .doc(docId)
        .collection('players')
        .count()
        .get();

    return count.toString();
  }

I am getting the error, bad state no elements. The first document in firebase is in Italics and apparently seems to be null when I read it. How can I fix it? Please advise.

Julia
  • 512
  • 6
MattPrash
  • 67
  • 5
  • Documents in italics don't exist and can't be read. The reason why the console displays them is because they still have nested documents that you should be able to navigate into. If you're not sure if the document exists, you'll have to check in your code if the read actually returned a document. – Doug Stevenson Aug 25 '23 at 21:18

0 Answers0