Dart/Flutter verification/validation in the Firebase Firestore database

Asked

Viewed 548 times

0

I’m trying to do a Dart/Flutter check/validation in the Firebase Firestore database to not save the same serial number string twice in the database, but I’m doing something wrong because it keeps saving the same field and value even though it’s the same.

Code

 var numero = Firestore.instance.collection("lockers").document().snapshots();

    if (numero  == "$_numeroSerie"){
      print("QR Code ja cadastrado");
    } else {
      await Firestore.instance
          .collection("lockers")
          .document()
          .setData({"numero_serie": _numeroSerie});
    }

Like this in the Firebase database inserir a descrição da imagem aqui

1 answer

1

This worked out !

    final QuerySnapshot result = await Future.value(Firestore.instance
        .collection("lockers")
        .where("numero_serie", isEqualTo: "$_numeroSerie")
        .limit(1)
        .getDocuments());

    final List<DocumentSnapshot> documents = result.documents;
    if (documents.length == 1) {
      confirmacao(context);
    } else {
      await Firestore.instance
          .collection("lockers")
          .document()
          .setData({"numero_serie": _numeroSerie});
    }
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.