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?

1
  • 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. Commented May 31, 2020 at 13:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This works, however it wont show items that don't have the property "category"
If the where clause is failing then it won't show any data
The query will show items if category == _category(your variable). If a document doesn't satisfy that then it wont retrieve it
let's put it like this. I want to be able to filter by category but I want it to show all items and don't filter, if my _category == null
works like a charm, not sure why I didn't think of this. amazing :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.