'How can I filter a stream in Flutter?

I am new to flutter/dart and within this page, I have a list of "information resources" populating the screen with a widget I've created called InformationResourceCard. I want to use a dropdown menu where users can select the information resource type and then the stream is filtered based on their selection.

I can't seem to figure out how to do this. My current understanding is that the page must be a stateful widget and that the dropdown menu selection will call setState to rebuild the list based on that selection. However, I don't know how to filter the stream. Below is the method I am using to build the list.

Widget _buildList(BuildContext context) {
    final database = Provider.of<Database>(context);
    return StreamBuilder<List<InformationResource>>(
      stream: database.informationResourceStream(widget.category.id),
      builder: (context, snapshot) {
        if (snapshot.hasData){
          final informationResources = snapshot.data;
          return ListView.builder(
            padding: const EdgeInsets.all(10.0),
            itemCount: informationResources.length,
            itemBuilder: (context, i) => InformationResourceCard(informationResource: informationResources[i])
          );
        }
        return //something else
      },
    );
  }

Any help would be greatly appreciated. Thanks!



Solution 1:[1]

The filtering should be done before passing the Stream to the StreamBuilder. In this case, the filter/query should be done on the Provider that returns the Stream. You can check this post on how you can implement a filter on a Provider.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omatt