I’m reading a user list from firebase and sending it to ListTile
, but the incoming data comes in alphabetical order. I want this data to come randomly. Does anyone know about this?
FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: (snapshot.data! as dynamic).docs.length,
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return InkWell(
onTap: (){},
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(
(snapshot.data! as dynamic).docs[index]['photoUrl'],
),
radius: 30,
),
trailing: ClipOval(
child: Container(
width: 10,
height: 10,
color: Colors.green,
),
),
title: Text(
(snapshot.data! as dynamic).docs[index]['username'], style: TextStyle(color: Colors.white, fontSize: 20),
),
subtitle: Text((snapshot.data! as dynamic).docs[index]['bio'],style: TextStyle(color: Colors.white70,fontSize: 14,)),
),
);
},
);
2
Answers
You can shuffle the list instead
Edit
In the codethat you edited you can create a variable that holds the list
Now you can use this final list inside the list builder
EDIT2
Try this code.
It is good practice to use variables instead of repeating long statements several times.
Bad
Good:
This will make your code more extensible for new features like what you are trying to do in a single clean line.
Full code: