简体   繁体   中英

How to filter a list of obseravble in stream flutter

I need to filter my list using the stream. I already have a stream that pulls all the clients, now I want to filter them out.

I have this code that is working normal, it pulls clients and displays me in listview. I just want to filter this data I get from the main stream. Can anyone with knowledge help me?

class ClientesControles extends BlocBase {


      final ClienteService clienteService;
      ClientesControles(this.clienteService);

      BuildContext _context;
      init(BuildContext context) {
        _context = context;
      }


      Observable<List<ClienteModel>> get clientesStream => clienteService.clientes;

      final _stringFiltroController = BehaviorSubject<String>();
      Observable<String> get stringFiltroFluxo => _stringFiltroController.stream;
      Sink<String> get stringFiltroEvent => _stringFiltroController.sink;

    @mustCallSuper
      void dispose() {
        _cadnomecliente.close();
        _cadnomefcliente.close();
        _cadtelcliente.close();
        _cademailcliente.close();
        _cadidcliente.close();
        _stringFiltroController.close();
      }
    }

    Class ClienteService{

     Observable<List<ClienteModel>> get clientes =>
          Observable(collection.snapshots().map((item) => item.documents
              .map<ClienteModel>((item) => ClienteModel.fromJson(item.data))
              .toList()));

    }

To filter a stream, add a where clause:

Observable<List<ClienteModel>> get clientes =>
          Observable(collection.snapshots().map((item) => item.documents
              .where((item) => hasWhatIWant(item))
              .map<ClienteModel>((item) => ClienteModel.fromJson(item.data))
             .toList()));

bool hasWhatIWant(item){
  //some check
}

I had a similar problem and fought it for 3 days until I found this post... Thanks... mine was just a bit different

Observable<List<Contact>> get contacts => _contactsListFetcher.stream
      .map((s) => s.where((c) => hasFilteredContent(c)).map((i) => i).toList());

  bool hasFilteredContent(Contact contact) {
    return contact.firstName.toLowerCase().contains(currentFilter.toLowerCase()) || contact.lastName.toLowerCase().contains(currentFilter.toLowerCase());
  }   

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