Luís Miguel
Luís Miguel

Reputation: 3

Filtering Firestore database

Inside the app I have 4 different Tabs and I want to show the filtered data from firestore.

I have the following Tabs:

The initial tab displays All data without filtering, the second tab displays the data that have tag 'music' and so on.

I copied the code 4 times and change the .where but this solution seems for me not the best one.

Here is my code of one of the tabs:

Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Center(
          child: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: 150.0,
              child: StreamBuilder<QuerySnapshot>(
                stream: Firestore.instance
                    .collection('eventsdata')
                    .orderBy('dateform')
                    .where('dateform', isGreaterThanOrEqualTo: _timeminus1)
                    .where('platform', isEqualTo: 'Comedy')
                    .snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (snapshot.hasError)
                    return new Text('Error: ${snapshot.error}');
                  switch (snapshot.connectionState) {
                    case ConnectionState.waiting:
                      return new Text(
                        'Loading...',
                        style:
                        TextStyle(fontSize: 15.0, color: Colors.black38),
                      );
                    default:
                      return Padding(
                        padding: const EdgeInsets.only(bottom: 179.0),
                        child: new ListView(

Can someone help me?

Upvotes: 0

Views: 212

Answers (1)

wcyankees424
wcyankees424

Reputation: 2654

If I understand your question correctly then your initial tabs displays all data unfiltered then I would save the data from this query and use it for the other screens. If you already have all your data on the device it makes more sense to filter the data on the device than to make multiple queries to firebase as you get charged per read / write. So I would save my data as a List<MyClass> then map that to a widget to display the data.

How to filter list in flutter?
Iterating through a list to render widegets

Here these aren't tutorials but they should point you in the right direction they where the best material I could find

ListView.builder(
        itemCount: myInfo.length,
        itemBuilder: (context, index) => InfoCard(myInfo[index]),
      ),

myInfo is your list and InfoCard is a reusable widget if you don't know about making reusable widget I attached a link if you don't need all that it can be as simple as a text widget.

Reusable Custom Widgets

Flutter: The power of small and reusable widgets

Upvotes: 1

Related Questions