Home > Back-end >  How to GET ALL Data from Firebase Firestore document and filter the field
How to GET ALL Data from Firebase Firestore document and filter the field

Time:02-23

So i have a Collection named "users" and document based on their authentication uid. I want to be able to go through ALL the users documents to find and display all the "full name" that have got a variable or field that is amphibianslvl3 > 80

firebase

How would i do this in flutter?

edit: So far this is what I have

    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(),
          );
        },
      ),
    ),
    );
  }
}

but it is not returning anything, screen is just black

amphibians level

CodePudding user response:

Try this:

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

Note: amphibianslvl3 should not be missing in any user

To use it in a widget check this example:

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(),
        );
      },
    )
  • Related