【问题标题】:Filtered list view not show up Flutter过滤后的列表视图不显示 Flutter
【发布时间】:2020-06-28 17:26:37
【问题描述】:

我尝试制作过滤列表视图,但是当我制作它时,过滤后的列表什么也没返回。请有人帮我解决它

这是我的搜索文本字段

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

【问题讨论】:

    标签: listview flutter dart


    【解决方案1】:

    我认为filteredList什么都不返回的原因是因为它不在setState

    试试

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

    【讨论】:

    • 整个应用程序?我的问题代码就是我发布的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2020-12-03
    • 2016-06-09
    相关资源
    最近更新 更多