mvasco
mvasco

Reputation: 5101

Filtering items with where on List is not getting any result

I am trying to implement a filter feature in a ListView.

I have included a TextField to insert the search String:

TextField(
              controller: _controller,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(15.0),
                hintText: 'Filter by name or NHC',
              ),
              onChanged: (string) {
                print(string);
                setState(() {

                });


              },
            ),

And then I have included a FutureBuilder to fetch the JSON string that should populate the ListView:

            child: FutureBuilder(
              future: fetchPacientes(value),
              builder: (context, snapshot) {

                if (snapshot.hasData) {
                  var filteredList = snapshot.data;
                  print("texto filtrado="+_controller.text);


                  filteredList = filteredList.where((element) => (element
                      .id_paciente == _controller.text ||
                      element.NHC ==
                          _controller.text));
                  return ListView.builder(
                    itemCount: filteredList.length,
                    shrinkWrap: true,
                    itemBuilder: (BuildContext context, index) {
                      Paciente paciente = filteredList[index];

The output from the list is working fine. The issue is that when implementing the filter, the result is empty.

This is the Paciente class:

import 'dart:convert';

List<Paciente> pacienteFromJson(String str) => List<Paciente>.from(json.decode(str).map((x) => Paciente.fromJson(x)));

String pacienteToJson(List<Paciente> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Paciente {
  Paciente({
    this.id_paciente,
    this.nombre_completo,
    this.apellidos,
    this.tel1,
    this.tel2,
    this.email,
    this.imagen_paciente,
    this.NHC

  });

  String id_paciente;
  String nombre_completo;
  String apellidos;
  String tel1;
  String tel2;
  String email;
  String imagen_paciente;
  String NHC;


  factory Paciente.fromJson(Map<String, dynamic> json) => Paciente(
    id_paciente: json["id_paciente"],
    nombre_completo: json["nombre_completo"],
    apellidos: json["apellidos"],
    tel1: json["tel1"],
    tel2: json["tel2"],
    email: json["email"],
    imagen_paciente: json["imagen_paciente"],
    NHC: json["NHC"]
  );

  Map<String, dynamic> toJson() => {
    "id_paciente": id_paciente,
    "nombre_completo": nombre_completo,
    "apellidos": apellidos,
    "tel1": tel1,
    "tel2": tel2,
    "email": email,
    "imagen_paciente": imagen_paciente,
    "NHC": NHC

  };
}

I guess the issue is due to the filtering method:

filteredList = filteredList.where((element) => (element
                      .id_paciente == _controller.text ||
                      element.NHC ==
                          _controller.text));

If I remove this line of code, the items are shown correctly. If I let this line of code, no item is shown.

What is wrong in that line of code?

Upvotes: 0

Views: 45

Answers (1)

Jigar Patel
Jigar Patel

Reputation: 5423

You should check if the text properties (id_paciente and NHC) contains the input text, not whether it is equal to the input text or not.

filteredList = filteredList.where((element) => (element
                     .id_paciente.contains(_controller.text) ||
                      element.NHC.contains(_controller.text)));

Edit:

To convert this to a List you need to use toList()

filteredList = filteredList.where((element) => (element
                         .id_paciente.contains(_controller.text) ||
                          element.NHC.contains(_controller.text))).toList();

Upvotes: 1

Related Questions