Kagan
Kagan

Reputation: 23

Flutter Firebase if document doesn't exist

I just want to say if the document exists setState(the first one) else setState(the second one). I'm new in flutter so please don't hate :)

Thanks for helping!

   Future<String> TerminBesetztOderFrei(String Friseur, String Tag, String Uhrzeit) async {
    await Firestore.instance.collection("$Friseur/1/$Tag/1/$Uhrzeit")
        .getDocuments()
        .then((querySnapshot) {
      querySnapshot.documents.forEach((result) {
        print(result.exists);
        setState(() {
          terminText = "Termin nicht verfügbar!";
          terminTextFarbe = Colors.red;
          buttonVisible = false;
        });
      });
    });
    setState(() {
      if(nameController.text != "" && telController.text != "") {
        terminText = "Termin verfügbar!";
        terminTextFarbe = Colors.green;
        buttonVisible = true;
      } else {
        terminText = "Termin verfügbar! Bitte Name und Telefon eingeben!";
        terminTextFarbe = Colors.green;
        buttonVisible = false;
      }
    });
  }

Upvotes: 2

Views: 1344

Answers (1)

Pavel Shastov
Pavel Shastov

Reputation: 3007

If you have the document Id:

  final docSnapshot = await Firestore.instance
    .collection("$Friseur/1/$Tag/1/$Uhrzeit")
    .document(${doc_id_here})
    .get();

    if(docSnapshot.exists) {
      setState(...)
    }
    else {
      setState(...)
    }

If you haven't

  final querySnapshot = await Firestore.instance
    .collection("$Friseur/1/$Tag/1/$Uhrzeit")
    .getDocuments();

    if(querySnapshot.documents.isNotEmpty) {
      setState(...)
    }

Upvotes: 3

Related Questions