1

is there a way to randomise data coming from firestore? i am using a streambuilder and a staggeredview.countbuilder . i could not find any docs about it! right now the data comes in the last signup order into the stream. can someone help? best regards everybody

this is my code

StreamBuilder(
  stream: Firestore.instance.collection('users').where('myCity', isEqualTo: city).snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return Text('Loading');
    return  Container(
      child: StaggeredGridView.countBuilder(
        crossAxisCount: 2,
          crossAxisSpacing: 12,
          mainAxisSpacing: 12,
          itemCount: snapshot.data.documents.length,
          itemBuilder: (BuildContext context, int index) {
            User user = User.fromDoc(snapshot.data
                .documents[index]);
            return GestureDetector(
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.transparent,
                borderRadius: BorderRadius.all(Radius.circular(12))

              ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(21),
                  child: Image.network(user.profileImageUrl,fit: BoxFit.cover,)
                ),
            )
            );
            },
        staggeredTileBuilder: (index){
          return StaggeredTile.count(0, index.isEven? 1.2 :1.4);
      },
          ),
    );
  },
);
  • 1
    "randomise data coming from firestore" What do you mean here? If you want to retrieve random documents from Firestore, have a look here: https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection – Frank van Puffelen Jun 30 '20 at 21:20

1 Answers1

2

You can use shuffle() method of the List class This will shuffle each time the stream emits a new value.

class MyApp extends StatelessWidget {
  @override
  Widget build(context) {
    return MaterialApp(
      home: Scaffold(
        body: StreamBuilder(
          stream: Firestore.instance
              .collection('users')
              .where('myCity', isEqualTo: city)
              .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return Text('Loading');
            final docs = snapshot.data.documents..shuffle();
            return Container(
              child: StaggeredGridView.countBuilder(
                crossAxisCount: 2,
                crossAxisSpacing: 12,
                mainAxisSpacing: 12,
                itemCount: docs.length,
                itemBuilder: (BuildContext context, int index) {
                  User user = User.fromDoc(docs[index]);
                  return GestureDetector(
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.transparent,
                          borderRadius: BorderRadius.all(Radius.circular(12))),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(21),
                        child: Image.network(
                          user.profileImageUrl,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  );
                },
                staggeredTileBuilder: (index) {
                  return StaggeredTile.count(0, index.isEven ? 1.2 : 1.4);
                },
              ),
            );
          },
        ),
      ),
    );
  }
}
Ahmed Erabti
  • 306
  • 1
  • 5