StreamBuilder and Firestore database

26 views Asked by At

I am was building a flutter application with some Sqflite and firestore database Now I have ran into a problem that whenever I run the application. I get snapshot.hasdata to true for the first time and than I get snapshot.hasdata to false in a second. I am attaching the screenshots of both console and the code here

StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection('ADs CommonGroup')
      .orderBy("id", descending: true)
      .orderBy("id", descending: true)
      .where('id', isEqualTo: databaseLogin[0]["number"])
      .snapshots(),
  builder: (
    BuildContext context,
    AsyncSnapshot<QuerySnapshot> snapshot,
  ) {
    print('snapshot does not have data ${snapshot.hasData}');
    print(databaseLogin[0]["number"]);

now the console prints this

I/flutter (16812): snapshot does not have data false
I/flutter (16812): +923335885631
I/flutter (16812): snapshot does not have data true
I/flutter (16812): +923335885631
I/flutter (16812): snapshot does not have data false
I/flutter (16812): +923335885631

What can I do code with Console

I think the snapshot.has data should return true as per the app was working like before

1

There are 1 answers

0
Frank van Puffelen On

The first step when working with an AsyncSnapshot is to handle errors. In a simple form:

builder: (
  BuildContext context,
  AsyncSnapshot<QuerySnapshot> snapshot,
) {
  if (snapshot.hasError) {
    print(snapshot.error);
    return Text("ERROR: ${snapshot.error}");
  }
  print('snapshot does not have data ${snapshot.hasData}');
  print(databaseLogin[0]["number"]);
  ...

This will likely tell you the cause of the problem.