我尝试制作过滤列表 View ,但是当我创建过滤列表 View 时,过滤后的内容什么也没有返回。请有人帮我解决
这是我的搜索文本字段
Container(
child: SizedBox(
height: 50,
child: TextField(
onChanged: (stringToSearch) {
print(stringToSearch);
setState(() {
filteredList = data.where((u) => (
u.first_name.toLowerCase().contains(stringToSearch.toLowerCase()) ||
u.last_name.toLowerCase().contains(stringToSearch.toLowerCase()))
).toList();
});
print(filteredList.toString());
},
controller: editingController,
decoration: InputDecoration(),
),
),
),
这是我的列表 View 生成器
child: ListView.builder(
padding: EdgeInsets.only(left:6, right: 6),
itemCount: data.isEmpty ? 0 : data.length,
itemBuilder: (BuildContext context, int index){
return Card(
elevation: 5,
color: data[index]['has_arrived']==0 ? Color(0xFFffb0b9) : Color(0xFF6bed96),
child: ListTile(
title: Text(data[index]['first_name']+' '+data[index]['last_name'], style: TextStyle(color: Colors.white),),
subtitle:
Container(
child: (
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(data[index]['email']??'-', style: TextStyle(fontSize: 13, color: data[index]['has_arrived']==0 ?Color(0xFFa10618):Color(0xFF007326))),
Text(data[index]['order_reference']??'-', style: TextStyle(fontSize: 12, color: Colors.white)),
Text(data[index]['phone_number']??'-', style: TextStyle(fontSize: 10, color: Colors.white)),
Text(data[index]['school']??'-', style: TextStyle(fontSize: 10, color: Colors.white)),
],
)
),
),
trailing:
InkWell(
onTap: () {
var _inOut = data[index]['has_arrived']==0?'in':'out';
var _privateReferenceNumber = data[index]['private_reference_number'];
_loadCheckInOut(_privateReferenceNumber, _inOut);
},
child: Badge(
elevation: 5,
padding: EdgeInsets.all(15),
badgeColor: data[index]['has_arrived']==0 ? Color(0xFF1e9e49) : Color(0xFFd13446),
badgeContent: Text(data[index]['has_arrived']==0 ?'Present':'Absent', style: TextStyle(color: Colors.white),),
toAnimate: false,
borderRadius: 20,
shape: BadgeShape.square,
),
),
isThreeLine: true,
),
);
},
),
我该怎么办?
print(filteredList.toString());
不返回任何内容。我想简化它,我不想做模型
最佳答案
我认为filteredList不返回任何内容的原因是因为它不在 setState 中
尝试
setState(() {
filteredList = data
.where((u) => (u.first_name
.toLowerCase()
.contains(stringToSearch.toLowerCase()) ||
u.last_name.toLowerCase().contains(stringToSearch.toLowerCase())))
.toList();
});
关于listview - 过滤后的 ListView 未显示Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725645/