I Have a CollectionGroup query which displays all document data from 'school_news' collection in a Listtile with Streambuilder and ListView.
But now I want to only show the document data in a ListView/Listtile where 'name' field equals name in a list , like :
List userSelections = ['Liebenberg', 'St Michaels' , 'School C' ];
FirebaseFirestore.instance
.collectionGroup('school_news')
.where('name', arrayContainsAny: userSelections )
.snapshots(),
So what I want now is to only then display the document data fields in a ListView/Listtile where the 'name' fields are Liebenberg, St Michaels and 'School C' instead of all the document data in 'school_news' collection .
Is something like that possible ? I tried with the code above but with no luck, So I am not sure what I'm missing .
Any help or guidance would be greatly appreciated .Thank you
UPDATE
I have updated as per answer and included full code , as below :
class UserHome extends StatefulWidget {
const UserHome({Key? key}) : super(key: key);
@override
_UserHomeState createState() => _UserHomeState();
}
class _UserHomeState extends State<UserHome> {
final uid = FirebaseAuth.instance.currentUser!.uid;
final db = FirebaseFirestore.instance;
List userSelections = ['Liebenberg', 'St Michaels'];
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collectionGroup('school_news')
.where('name', whereIn: userSelections)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: Text('Loading '),
);
} else
return ListView(
children: snapshot.data!.docs.map((doc) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: GestureDetector(
onTap: () {},
child: Column(
children: [
ListTile(
leading: Text(doc['title']),
),
SizedBox(
height: 10,
),
],
),
),
);
}).toList(),
);
},
),
);
}
}
Seems like no data is returned as the page just displays 'Loading' . The link for creating the new index for the 'WhereIn' I also dont get .
Anything I am doin wrong ?
Thanks for the help so far .
