skip to Main Content
My code is as follows:

  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().set({'test': 'test'});

    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 looking to get a actual string value instead of AggregateQuerySnapshot. Can someone please help me out? Thank you so much in advance.
Regards,
Matt

I am looking to get a actual string value instead of AggregateQuerySnapshot. Can someone please help me out? Thank you so much in advance.
Regards,
Matt

I am looking to get a actual string value instead of AggregateQuerySnapshot. Can someone please help me out? Thank you so much in advance.
Regards,
Matt

2

Answers


  1. Chosen as BEST ANSWER

    My edited code is as below:

    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 another error: bad state, no element. The first document in firebase is also in Italics. Apparently its showing as null. How can I fix this?


  2. From the documentation on getting the count in Flutter:

    // Returns number of documents in users collection
    db.collection("users").count().get().then(
          (res) => print(res.count),
          onError: (e) => print("Error completing: $e"),
        );
    

    So you need to call count.count rather than count.toString().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search