简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM