0

I have a Firestore structure enter image description here what i want is i want to get count of all the members that are enrolled within a user. But Recursive call to below function is just counting the initial array count.

Future<int> getalldata(String docref) async {
    int count = 0, abhj = 0;
    DocumentReference docRef =
        FirebaseFirestore.instance.collection('memberships').doc(docref);

    DocumentSnapshot value = await docRef.get();
    print(value.data().toString());
    if (value.data().toString().contains('referredto:')) {
      print("CHala");
      if (value.get('referredto').length > 0) {
        print(value.get('referredto').length);

        for (dynamic i in value.get('referredto')) {
          count += await getalldata(i.id.toString());
        }

        //print('its empty' + value.get('referredto')[0].id.toString())
      } else {
        count = await count + 1;
        print('ye b1');
      }
    } else {
      count = await count + 1;
      print('ye b');
    }

    print("count>>" + count.toString());
    return await count;
  }

I want recursive function which can count all the memberships. But i am getting just the count in the initial Call.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 1
    1. You are `await`ing various things that aren't `Future`s (e.g. `await count`). Enable the [`unawaited_futures`](https://dart.dev/tools/linter-rules#unawaited_futures) and [`await_only_futures`](https://dart.dev/tools/linter-rules#await_only_futures) lints instead of just sprinkling `await` everywhere. 2. What printed output do you get? Are you sure that your recursive function is executing the code paths you expect? 3. If an a value includes a `referredto` list, you currently are counting only the children but not the parent itself. Is that what you intend? – jamesdlin Mar 05 '23 at 16:50
  • I suggest you work with Firebase Cloud Functions and listen to CRUD operations. Then you can easily update count for whatever you want using `FieldValue.increment` – Georgina Mar 06 '23 at 12:26

0 Answers0