代码之家  ›  专栏  ›  技术社区  ›  LaurenTsai

如何从Firebase Firestore文档中获取所有数据并筛选字段

  •  0
  • LaurenTsai  · 技术社区  · 4 年前

    因此,我有一个名为“用户”的集合,并根据他们的身份验证uid创建了文档。我希望能够浏览所有用户文档,找到并显示所有“全名”,其中包含一个变量或字段,该变量或字段名为ANSYSLVL3>80

    firebase

    在弗利特我该怎么做?

    编辑: 到目前为止,这就是我所拥有的

        class amphLeaderboard extends StatefulWidget {
      @override
      _amphLeaderboardState createState() => _amphLeaderboardState();
    }
    
    class _amphLeaderboardState extends State<amphLeaderboard> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.black,
          appBar: AppBar(
          title: Text(
          'Amphibians 100% Completion',
          style: GoogleFonts.sora(
              textStyle: TextStyle(
                /*fontWeight: FontWeight.bold,*/
                fontSize: 18,
                color: mywhite,
              )),
        ),
        automaticallyImplyLeading: false,
        centerTitle: true,
        elevation: 0,
        ),
          body: Center(
          child: FutureBuilder<QuerySnapshot>(
            future: FirebaseFirestore.instance
                .collection("users")
                .where("amphibianslvl3", isGreaterThan: 80)
                .get(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return CircularProgressIndicator();
              }
              return ListView(
                children: snapshot.data.docs.map(
                      (e) {
                    return Text((e.data() as Map)["userName"], style: TextStyle(fontSize: 20, color: Colors.white),);
                  },
                ).toList(),
              );
            },
          ),
        ),
        );
      }
    }
    

    但它没有返回任何东西,屏幕只是黑色的

    amphibians level

    1 回复  |  直到 4 年前
        1
  •  0
  •   Josteve Adekanbi    4 年前

    试试这个:

    FirebaseFirestore.instance.collection("users").where("amphibianslvl3", isGreaterThan: 80).get();
    

    注: amphibianslvl3 不应在任何用户中丢失

    要在小部件中使用它,请查看以下示例:

    FutureBuilder<QuerySnapshot>(
          future: FirebaseFirestore.instance
              .collection("users")
              .where("amphibianslvl3", isGreaterThan: 80)
              .get(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return CircularProgressIndicator();
            }
    
            return ListView(
              children: snapshot.data!.docs.map(
                (e) {
                  return Text((e.data() as Map)["fulName"]);
                },
              ).toList(),
            );
          },
        )