I am trying to implement search functionality in my flutter application. I have tried but it did not work. When I print() the data I am searching for is being printed in debug console but not updating on the User Interface.
I have a textfield inside a futurebuilder and the listview builder. when i type something in the text field it does not reflect on the UI.
Here is my code.
Padding(
padding: const EdgeInsets.all(10.00),
child: FutureBuilder(
future: SupervisorServices.getWorkersList(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<dynamic> data = snapshot.data['Hamal'].toList();
foundHamals = data;
if (data.isEmpty) {
return const NotFound();
}
return RefreshIndicator(
onRefresh: () async {
setState(() {});
return SupervisorServices.getWorkersList();
},
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.search),
hintText: "Search Hamal"),
onChanged: (val) {
if (val.isEmpty) {
foundHamals = data;
} else {}
setState(() {
foundHamals = data
.where((element) =>
element['worker_name']
.toLowerCase()
.contains(val.toLowerCase()))
.toList();
});
},
),
ListView.builder(
itemCount: foundHamals.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Card(
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SupWorkerView(
showAppbar: true,
workerId: foundHamals[index]
['worker_id']),
),
);
},
child: ListTile(
leading: SizedBox(
height: double.infinity,
child: foundHamals[index]
['worker_image'] ==
null
? const CircleAvatar(
radius: 30, // Image radius
backgroundImage: AssetImage(
"assets/images/default-user.webp"),
)
: CircleAvatar(
radius: 30, // Image radius
backgroundImage: NetworkImage(
'http://10.0.2.2:8000/uploads/workers/${foundHamals[index]['worker_image']}'),
),
),
subtitle: Row(children: [
const Icon(
FeatherIcons.mapPin,
color: Colors.blue,
size: 12,
),
const SizedBox(
width: 5,
),
Text(foundHamals[index]
['worker_address']),
]),
title: Text(
foundHamals[index]['worker_name']),
trailing: IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
SupWorkerView(
showAppbar: true,
workerId:
foundHamals[index]
['worker_id']),
),
);
},
icon: const Icon(
FeatherIcons.chevronRight,
color: Colors.blue,
),
),
),
),
);
},
),
],
),
);
} else if (snapshot.hasError) {
return const Center(
child: Text("Something went wrong !"),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
),