raiton
raiton

Reputation: 121

Where clauses in flutter firebase streams

I'm setting up a category selector where pressing a certain image changes the category the user is supposed to see. however if the user doesn't pick a category he should see all items.

 FutureBuilder(
          future: _authService.getUserId(),
          builder: (context, snapshot) {
            if (snapshot.hasData)
              return StreamBuilder(
                  stream: _firestore
                      .collection('Item')
                      .where(_category != null ? ('category', isEqualTo: _category) : true)
                      .snapshots(),

I was trying to do something like this but it just gives me an error saying it was expecting a bracket somewhere..

Another question I have about this setup is, even if the _categoy changes, would that update the list? or since the stream is already built it won't? and in the latter, how would I come about updating the list with the actual values?

Upvotes: 1

Views: 1555

Answers (2)

awaik
awaik

Reputation: 12335

The actual answer is here https://stackoverflow.com/a/63293409/7198006

that is

Dont initialize streams in build method

Use StatefulWidget and initialize the stream in State.initState

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80924

Change it to the following:

          StreamBuilder(
            stream: _category != null
                ? Firestore.instance
                    .collection('Item')
                    .where("category", isEqualTo: _category)
                    .snapshots()
                : Firestore.instance.collection('Item').snapshots(),
          ),

If the _category is not null then use the where clause, else retrieve everything inside Item.

Upvotes: 2

Related Questions