Home > Net >  Flutter Stream List Filter
Flutter Stream List Filter

Time:09-24

I am building an app that searches for specific bluetooth devices. I am using FlutterBlue and so my code looks like this:

StreamBuilder<List<ScanResult>>(
    stream: FlutterBlue.instance.scanResults

The problem is that I want to filter out the results based on some criteria. scanResults is type Stream<List<ScanResult>> and with a normal List I would do .where((element) => true/false) but when I do .where the element is actually type List instead of the ScanResult which doesn't make sense.

Can't figure out how I am supposed to do this. I've searched around to no avail. I must be missing a setup to maintain the Stream but be able to rebuild the list based on a .where

CodePudding user response:

To filter on the list you need to filter on the snapshot data provided in the StreamBuilder builder directly.

Here is a small example:

StreamBuilder<List<ScanResult>>(
  stream: FlutterBlue.instance.scanResults,
  builder: (_, snapshot) {
    if (!snapshot.hasData) {
      return LoadingWidget()
    }

    final filteredResult = snapshot.data!.where((element) => ...);
  },
);
  • Related