Access data in Firebase with Flutter

Asked

Viewed 81 times

-1

I am trying to access the data according to the image. I need to bring the information that is inside the document. I’m trying to use the following code:

final QuerySnapshot result1 = await Future.value(Firestore.instance
          .collection("pesquisas").getDocuments());
final List<DocumentSnapshot> documents1 = result1.documents;
print(documents1.elementAt(0).documentID.toString());

But only returns the document, in case 02022021. But I need to access the fields, the data that is in this document to fill a list. Follow the image: inserir a descrição da imagem aqui

I thank and await return!

  • 1

    Edit your question and add the code you used to fetch the data, to understand better. (There you only put a line)

  • Thanks for the feedback. Updated!

1 answer

0

I do not know if it is the best solution, but I answered!

void _addDate() async {

    DateTime today = new DateTime.now();
    String dataCorreta =
        "${today.day.toString().padLeft(2, '0')}${today.month.toString().padLeft(2, '0')}${today.year.toString()}";

    final QuerySnapshot result = await Future.value(Firestore.instance
        .collection("pesquisas")
        .where(FieldPath.documentId, isEqualTo: dataCorreta)
        .limit(1)
        .getDocuments());

    final List<DocumentSnapshot> documents = result.documents;
    if (documents.length == 0) {
      Firestore.instance
          .collection("pesquisas")
          .document(dataCorreta)
          .setData(data);
    } else {
      documents.forEach((element) {
        setState(() {
          _counterDissatisfied =
              int.parse(element.data.values.elementAt(0).toString());
          _counterVerySatisfied =
              int.parse(element.data.values.elementAt(2).toString());
          _counterSatisfied =
              int.parse(element.data.values.elementAt(3).toString());
        });
      });
    }
  }

Browser other questions tagged

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