Reputation: 3
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()));
}
Upvotes: 0
Views: 2708
Reputation: 53
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());
}
Upvotes: 0
Reputation: 619
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
}
Upvotes: 1