0

im trying this and it doesnt work:

  Future<bool> isGroupNameAvailable(String name) async {
bool result = true;
(await _fireStore.collection("groups").get()).docs.forEach((doc) {
  if (name == doc.id) {
    result = false;
  }
});
return result;

}

 FutureBuilder(
                          future: db!.isGroupNameAvailable(value),
                          builder: (BuildContext context,
                              AsyncSnapshot<bool> snap) {
                            setState(() {
                              _isGidUnique = snap.data!
                                  ? null
                                  : "this group name is taken,try different one";
                            });

                            print("*************first" +
                                _isGidUnique.toString());
                            return Text("");
                          },
                        );

                        // db!
                        //     .isGroupNameAvailable(value)
                        //     .then((avilible) => setState(() {
                        //           _isGidUnique = avilible
                        //               ? null
                        //               : "this group name is taken,try different one";
                        //         }));
                        print(_isGidUnique);
                        return _isGidUnique;

_isGidUnique should be the string "this group name is taken,try different one" but it gets null i think the future builder doesnt work from some reason help :(

  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Aug 26 '21 at 14:29
  • hi, thank you but not really. i understand future concept, but i dont really understand why my code is not working – Harel Moshayof Aug 26 '21 at 16:37
  • Have a close look at the FutureBuilder example there. You are missing the fact that SnapShot might not have data yet. – nvoigt Aug 26 '21 at 16:39

1 Answers1

-2

You should try to use the Future.wait method as follows:

/// Future.wait takes an array of futures and completes when they are all over
var docs = await Future.wait(_fireStore.collection("groups").get()).docs);
/// every returns true if all elements match the given predicate
return docs.every((doc) => name != doc.id);
malrok44
  • 592
  • 6
  • 17