A
A
Artem4412020-11-21 14:08:18
Android
Artem441, 2020-11-21 14:08:18

How to get data from firestore (flutter)?

I'm writing my first application on flutter and I'm having trouble learning the backend.

class Content {

  int index;

  Content({this.index});


  Stream<QuerySnapshot> streamCards = FirebaseFirestore.instance
      .collection('cards')
      .snapshots();


  text(){



    text(AsyncSnapshot<QuerySnapshot> snapshot) {


      if (index > snapshot.data.docs.length - 1) {
        index = snapshot.data.docs.length - 1;
        return snapshot.data.docs[index]['desc'];

      } else {

        return snapshot.data.docs[index]['desc'];
      }
    }

    return StreamBuilder(
        stream: streamCards,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            return Text('${
                text(snapshot)
            }');
          } else if (snapshot.hasError) {
            return Text('error');
          } else {
            return Text('Loading');
          }
        }
    );


  }
}


in the application itself, at the click of a button, I change the index and the content ("desc") from the database changes accordingly.

The question is how to put a limit on the number of documents in the database?

If there is a more rational execution of the decision, I will be glad))))

thanks for understanding))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question