I made a follower list. But it lists the profile from the same id 5 times. As seen in the photo below. I couldn't find a solution. Can you help me. My codes are as follows.....................................................................................................................................................................
Follow_card
class _FollowCardState extends State<FollowCard> {
List<dynamic> followList = [];
getdata() async {
List<dynamic> followers = [];
final currentUserSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.get();
List<String> followIds =
List.from(currentUserSnapshot.data()!['following']);
// loop through all ids and get associated user object by userID/followerID
for (int i = 0; i < followIds.length; i ) {
var followId = followIds[i];
var data = await FirebaseFirestore.instance
.collection('users')
.doc(followId)
.get();
followers.add(data);
}
setState(() => followList = followers);
}
@override
void initState() {
super.initState();
getdata();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: ListView.builder(
shrinkWrap: true,
itemCount: followList.length,
itemBuilder: (context, index) {
var followerItem = followList[index];
print('photoUrl');
return _buildFollowersCard(followerItem['photoUrl'],
followerItem['username'], followerItem['uid']);
}),
);
}
Widget _buildFollowersCard(String photoUrl, String username, String uid) {
return Container(
height: 70,
width: double.infinity,
color: mobileBackgroundColor,
child: Card(
child: Column(children: [
//Header
Container(
height: 40,
width: double.infinity,
padding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 16,
).copyWith(right: 0),
child: Row(
children: [
CircleAvatar(
radius: 16,
backgroundImage: NetworkImage(
photoUrl,
),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ProfileScreen(uid: uid)));
},
child: Text(
username,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
],
),
),
),
],
),
)
]),
),
);
}
}
Follow_screen
class _FollowScreenState extends State<FollowScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: mobileBackgroundColor,
centerTitle: true,
title: Image.asset(
'Resim/logo.png',
height: 50,
),
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => FollowCard(
snap: snapshot.data!.docs[index].data(),
),
);
},
),
);
}
}
CodePudding user response:
Convert the list into Set and then convert it again to list to eliminate the redundant element like :
getdata() async { List followers = [];
final currentUserSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.get();
List<String> followIds =
List.from(currentUserSnapshot.data()!['following']);
// loop through all ids and get associated user object by userID/followerID
for (int i = 0; i < followIds.length; i ) {
var followId = followIds[i];
var data = await FirebaseFirestore.instance
.collection('users')
.doc(followId)
.get();
followers.add(data);
}
// Add this line
followers = followers.toSet().toList();
setState(() => followList = followers);
}
CodePudding user response:
I think the problem is that FollowScreen
is passing the user as a parameter to FollowCard
(the snap
property) but it's not being used anywhere inside it. Maybe the problem is exactly this because FollowCard
is fetching again the users instead of using the snap
user. This way FollowCard
lists all followers from the first user it finds.
To fix this just use the snap
as the follower user. It's going to be something like this:
class FollowScreen extends StatefulWidget {
final String userId;
const FollowScreen({required this.userId, Key? key}) : super(key: key);
@override
State<FollowScreen> createState() => _FollowScreenState();
}
class _FollowScreenState extends State<FollowScreen> {
List<DocumentSnapshot<Map<String, dynamic>>> followList = [];
late final Future _future;
getdata() async {
final user = await FirebaseFirestore.instance
.collection('users')
.doc(widget.userId)
.get();
List<DocumentSnapshot<Map<String, dynamic>>> followers = [];
List<String> followIds = List.from(user['following']);
// loop through all ids and get associated user object by userID/followerID
for (int i = 0; i < followIds.length; i ) {
var followId = followIds[i];
var data = await FirebaseFirestore.instance
.collection('users')
.doc(followId)
.get();
followers.add(data);
}
setState(() => followList = followers);
}
@override
void initState() {
super.initState();
_future = getdata();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// backgroundColor: mobileBackgroundColor,
centerTitle: true,
title: Image.asset(
'Resim/logo.png',
height: 50,
),
),
body: FutureBuilder(
future: _future,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
followList.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: followList.length,
itemBuilder: (context, index) => FollowCard(
snap: followList[index],
),
);
},
),
);
}
}
class FollowCard extends StatefulWidget {
final DocumentSnapshot<Map<String, dynamic>> snap;
const FollowCard({required this.snap, Key? key}) : super(key: key);
@override
State<FollowCard> createState() => _FollowCardState();
}
class _FollowCardState extends State<FollowCard> {
@override
Widget build(BuildContext context) {
return _buildFollowersCard(
widget.snap['photoUrl'], widget.snap['username'], widget.snap['uid']);
}
Widget _buildFollowersCard(String photoUrl, String username, String uid) {
return Container(
height: 70,
width: double.infinity,
// color: mobileBackgroundColor,
child: Card(
child: Column(children: [
//Header
Container(
height: 40,
width: double.infinity,
padding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 16,
).copyWith(right: 0),
child: Row(
children: [
CircleAvatar(
radius: 16,
backgroundImage: NetworkImage(
photoUrl,
),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Container()));
},
child: Text(
username,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
],
),
),
),
],
),
)
]),
),
);
}
}