StreamBuilder 和 Firestore 数据库

问题描述 投票:0回答:1

我正在使用一些 Sqflite 和 firestore 数据库构建一个 flutter 应用程序 现在我遇到了一个问题,每当我运行该应用程序时。我第一次将 snapshot.hasdata 设置为 true,然后立即将 snapshot.hasdata 设置为 false。我在此处附上控制台和代码的屏幕截图

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"]);

现在控制台打印出这个

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

我能做什么 code with Console

我认为 snapshot.has 数据应该返回 true,因为应用程序像以前一样工作

flutter firebase google-cloud-firestore sqflite stream-builder
1个回答
0
投票

使用

AsyncSnapshot
时的第一步是处理错误。以简单的形式:

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"]);
  ...

这可能会告诉您问题的原因。

© www.soinside.com 2019 - 2024. All rights reserved.