过滤后的列表视图未显示Flutter

时间:2020-03-17 15:40:55

标签: listview flutter dart

我尝试过创建过滤列表视图,但是当我创建过滤列表视图时,过滤后的内容什么也不返回。请有人帮我解决

这是我的搜索文本字段

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(),
                ),
              ),
            ),

这是我的列表视图生成器

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());不返回任何内容。我想简化它,我不想做模型

1 个答案:

答案 0 :(得分:0)

我认为filteredList不返回任何内容的原因是因为它不在 setState

尝试

  setState(() {
    filteredList = data
        .where((u) => (u.first_name
                .toLowerCase()
                .contains(stringToSearch.toLowerCase()) ||
            u.last_name.toLowerCase().contains(stringToSearch.toLowerCase())))
        .toList();
  });
相关问题