1

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?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
raiton
  • 101
  • 11
  • 1) "it just gives me an error saying it was expecting a bracket somewhere.." Please edit your question to include the exact error message and (if this is a run time error) stack trace. 2) What is the value of `_category`. Please make sure the code can be run as is, so that everything needed is in the snippet. 3) Please limit yourself to a single question per post. – Frank van Puffelen May 31 '20 at 13:56

2 Answers2

2

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

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

awaik
  • 10,143
  • 2
  • 44
  • 50