英文:
search bar I can't find anything
问题
我是新手编程者,我一直在尝试编写搜索栏的代码,但我发现我的搜索栏存在问题,它似乎无法搜索我想要的内容,好像什么都找不到。我尝试修复了一段时间,但仍然遇到了相同的问题。我从不同的网站学习了一些东西,然后进行了修复,但仍然卡在了同一个问题上,我认为也许我将代码写在了错误的地方,但我的代码没有任何错误。
类 view_problem 扩展 StatefulWidget {
  @override
  State<view_problem> createState() => _view_problemState();
}
类 _view_problemState 扩展 State<view_problem> {
  TextEditingController _searchController = TextEditingController();
  List<problemModel> problemlist = [];
  StreamController _streamController = StreamController();
  Future getAllProblem() async {
    problemlist = await problemcontrollers().getProblem();
    _streamController.sink.add(problemlist);
  }
  @override
  void initState() {
    // TODO: implement initState
    Timer.periodic(Duration(seconds: 1), (timer) {
      getAllProblem();
    });
    super.initState();
  }
  void _filterSearch(String query) {
    List<problemModel> filteredList = [];
    for (var item in problemlist) {
      if (item.toString().contains(query.toString())) {
        filteredList.add(item);
      }
    }
    setState(() {
      problemlist = filteredList;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 14, 12, 134),
        title: const Text('show information'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(
                onChanged: (value) {
                  _filterSearch(value);
                },
                controller: _searchController,
                decoration: InputDecoration(
                    labelText: "search",
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(25.0)))),
              ),
            ),
            Expanded(
              child: StreamBuilder(
                stream: _streamController.stream,
                builder: (context, snapshots) {
                  if (snapshots.hasData) {
                    return ListView.builder(
                        itemCount: problemlist.length,
                        itemBuilder: ((context, index) {
                          problemModel problem = problemlist[index];
                          return Card(
                            margin: EdgeInsets.all(10),
                            child: ListTile(
                              title: Text(
                                problem.name_surname,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 19),
                              ),
                              subtitle: Text(
                                problem.area,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                              trailing: Text(
                                problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                            ),
                          );
                        }));
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
英文:
I'm new to coding and I've been trying to code the search bar for a while now but I've found that my search bar has a problem where it doesn't search what I want, it seems like it can't find anything. I tried to fix it for a while but still got the same problem. I studied from different websites. and then fix it, still stuck on the same problem, I think maybe I wrote the code in a wrong place but my code doesn't have any error.enter image description here
class view_problem extends StatefulWidget {
@override
State<view_problem> createState() => _view_problemState();
}
class _view_problemState extends State<view_problem> {
TextEditingController _searchController = TextEditingController();
List<problemModel> problemlist = [];
StreamController _streamController = StreamController();
Future getAllProblem() async {
problemlist = await problemcontrollers().getProblem();
_streamController.sink.add(problemlist);
}
@override
void initState() {
// TODO: implement initState
Timer.periodic(Duration(seconds: 1), (timer) {
getAllProblem();
});
super.initState();
}
void _filterSearch(String query) {
List<problemModel> filteredList = [];
for (var item in problemlist) {
if (item.toString().contains(query.toString())) {
filteredList.add(item);
}
}
setState(() {
problemlist = filteredList;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 14, 12, 134),
title: const Text('show information'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
onChanged: (value) {
_filterSearch(value);
},
controller: _searchController,
decoration: InputDecoration(
labelText: "search",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
),
Expanded(
child: StreamBuilder(
stream: _streamController.stream,
builder: (context, snapshots) {
if (snapshots.hasData) {
return ListView.builder(
itemCount: problemlist.length,
itemBuilder: ((context, index) {
problemModel problem = problemlist[index];
return Card(
margin: EdgeInsets.all(10),
child: ListTile(
title: Text(
problem.name_surname,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 19),
),
subtitle: Text(
problem.area,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
trailing: Text(
problem.status,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
),
);
}));
}
return Center(
child: CircularProgressIndicator(),
);
},
),
)
],
),
),
);
}
}
答案1
得分: 0
你有一个定时器每秒定期运行,永远获取所有问题,这会覆盖任何过滤器。我认为你不想要那个定时器,只需在initState中调用一次getAllProblem()。然后将originalList保存在另一个变量中。类似这样
class _view_problemState extends State<view_problem> {
  TextEditingController _searchController = TextEditingController();
  List<problemModel> problemlist = [];
  List<problemModel> originalList = [];
  StreamController _streamController = StreamController();
  Future getAllProblem() async {
    problemlist = await problemcontrollers().getProblem();
    originalList = problemlist;
    _streamController.sink.add(problemlist);
  }
  @override
  void initState() {
    getAllProblem();
    super.initState();
  }
  void _filterSearch(String query) {
    List<problemModel> filteredList = [];
    for (var item in originalList) {
      if (item.toString().contains(query.toString())) {
        filteredList.add(item);
      }
    }
    setState(() {
      problemlist = filteredList;
    });
  }
   @override
   Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 14, 12, 134),
        title: const Text('show information'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(
                onChanged: (value) {
                  _filterSearch(value);
                },
                controller: _searchController,
                decoration: InputDecoration(
                    labelText: "search",
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(25.0)))),
              ),
            ),
            Expanded(
              child: StreamBuilder(
                stream: _streamController.stream,
                builder: (context, snapshots) {
                  if (snapshots.hasData) {
                    return ListView.builder(
                        itemCount: problemlist.length,
                        itemBuilder: ((context, index) {
                           problemModel problem = problemlist[index];
                          return Card(
                            margin: EdgeInsets.all(10),
                            child: ListTile(
                              title: Text(
                                problem.name_surname,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 19),
                              ),
                              subtitle: Text(
                                problem.area,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                              trailing: Text(
                                 problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                            ),
                          );
                        }));
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            )
          ],
        ),
       ),
     );
  }
}
英文:
You have a timer running periodically running every second forever that gets all problems, which will overwrite any filtering. I don't think you want that timer there, and just do getAllProblem(); once in the initState. And save the originalList in another variable. Something like this
class _view_problemState extends State<view_problem> {
TextEditingController _searchController = TextEditingController();
List<problemModel> problemlist = [];
List<problemModel> originalList = [];
StreamController _streamController = StreamController();
Future getAllProblem() async {
problemlist = await problemcontrollers().getProblem();
originalList = problemlist;
_streamController.sink.add(problemlist);
}
@override
void initState() {
getAllProblem();
super.initState();
}
void _filterSearch(String query) {
List<problemModel> filteredList = [];
for (var item in originalList) {
if (item.toString().contains(query.toString())) {
filteredList.add(item);
}
}
setState(() {
problemlist = filteredList;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 14, 12, 134),
title: const Text('show information'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
onChanged: (value) {
_filterSearch(value);
},
controller: _searchController,
decoration: InputDecoration(
labelText: "search",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
),
Expanded(
child: StreamBuilder(
stream: _streamController.stream,
builder: (context, snapshots) {
if (snapshots.hasData) {
return ListView.builder(
itemCount: problemlist.length,
itemBuilder: ((context, index) {
problemModel problem = problemlist[index];
return Card(
margin: EdgeInsets.all(10),
child: ListTile(
title: Text(
problem.name_surname,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 19),
),
subtitle: Text(
problem.area,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
trailing: Text(
problem.status,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
),
);
}));
}
return Center(
child: CircularProgressIndicator(),
);
},
),
)
],
),
),
);
}
}
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论