搜索栏 我找不到任何东西

huangapple go评论103阅读模式
英文:

search bar I can't find anything

问题

  1. 我是新手编程者,我一直在尝试编写搜索栏的代码,但我发现我的搜索栏存在问题,它似乎无法搜索我想要的内容,好像什么都找不到。我尝试修复了一段时间,但仍然遇到了相同的问题。我从不同的网站学习了一些东西,然后进行了修复,但仍然卡在了同一个问题上,我认为也许我将代码写在了错误的地方,但我的代码没有任何错误。
  2. view_problem 扩展 StatefulWidget {
  3. @override
  4. State<view_problem> createState() => _view_problemState();
  5. }
  6. _view_problemState 扩展 State<view_problem> {
  7. TextEditingController _searchController = TextEditingController();
  8. List<problemModel> problemlist = [];
  9. StreamController _streamController = StreamController();
  10. Future getAllProblem() async {
  11. problemlist = await problemcontrollers().getProblem();
  12. _streamController.sink.add(problemlist);
  13. }
  14. @override
  15. void initState() {
  16. // TODO: implement initState
  17. Timer.periodic(Duration(seconds: 1), (timer) {
  18. getAllProblem();
  19. });
  20. super.initState();
  21. }
  22. void _filterSearch(String query) {
  23. List<problemModel> filteredList = [];
  24. for (var item in problemlist) {
  25. if (item.toString().contains(query.toString())) {
  26. filteredList.add(item);
  27. }
  28. }
  29. setState(() {
  30. problemlist = filteredList;
  31. });
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. appBar: AppBar(
  37. backgroundColor: Color.fromARGB(255, 14, 12, 134),
  38. title: const Text('show information'),
  39. ),
  40. body: Container(
  41. child: Column(
  42. children: <Widget>[
  43. Padding(
  44. padding: const EdgeInsets.all(8.0),
  45. child: TextField(
  46. onChanged: (value) {
  47. _filterSearch(value);
  48. },
  49. controller: _searchController,
  50. decoration: InputDecoration(
  51. labelText: "search",
  52. prefixIcon: Icon(Icons.search),
  53. border: OutlineInputBorder(
  54. borderRadius: BorderRadius.all(Radius.circular(25.0)))),
  55. ),
  56. ),
  57. Expanded(
  58. child: StreamBuilder(
  59. stream: _streamController.stream,
  60. builder: (context, snapshots) {
  61. if (snapshots.hasData) {
  62. return ListView.builder(
  63. itemCount: problemlist.length,
  64. itemBuilder: ((context, index) {
  65. problemModel problem = problemlist[index];
  66. return Card(
  67. margin: EdgeInsets.all(10),
  68. child: ListTile(
  69. title: Text(
  70. problem.name_surname,
  71. style: TextStyle(
  72. fontWeight: FontWeight.bold, fontSize: 19),
  73. ),
  74. subtitle: Text(
  75. problem.area,
  76. style: TextStyle(
  77. fontWeight: FontWeight.bold, fontSize: 17),
  78. ),
  79. trailing: Text(
  80. problem.status,
  81. style: TextStyle(
  82. fontWeight: FontWeight.bold, fontSize: 16),
  83. ),
  84. ),
  85. );
  86. }));
  87. }
  88. return Center(
  89. child: CircularProgressIndicator(),
  90. );
  91. },
  92. ),
  93. )
  94. ],
  95. ),
  96. ),
  97. );
  98. }
  99. }
英文:

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

  1. class view_problem extends StatefulWidget {
  2. @override
  3. State&lt;view_problem&gt; createState() =&gt; _view_problemState();
  4. }
  5. class _view_problemState extends State&lt;view_problem&gt; {
  6. TextEditingController _searchController = TextEditingController();
  7. List&lt;problemModel&gt; problemlist = [];
  8. StreamController _streamController = StreamController();
  9. Future getAllProblem() async {
  10. problemlist = await problemcontrollers().getProblem();
  11. _streamController.sink.add(problemlist);
  12. }
  13. @override
  14. void initState() {
  15. // TODO: implement initState
  16. Timer.periodic(Duration(seconds: 1), (timer) {
  17. getAllProblem();
  18. });
  19. super.initState();
  20. }
  21. void _filterSearch(String query) {
  22. List&lt;problemModel&gt; filteredList = [];
  23. for (var item in problemlist) {
  24. if (item.toString().contains(query.toString())) {
  25. filteredList.add(item);
  26. }
  27. }
  28. setState(() {
  29. problemlist = filteredList;
  30. });
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. backgroundColor: Color.fromARGB(255, 14, 12, 134),
  37. title: const Text(&#39;show information&#39;),
  38. ),
  39. body: Container(
  40. child: Column(
  41. children: &lt;Widget&gt;[
  42. Padding(
  43. padding: const EdgeInsets.all(8.0),
  44. child: TextField(
  45. onChanged: (value) {
  46. _filterSearch(value);
  47. },
  48. controller: _searchController,
  49. decoration: InputDecoration(
  50. labelText: &quot;search&quot;,
  51. prefixIcon: Icon(Icons.search),
  52. border: OutlineInputBorder(
  53. borderRadius: BorderRadius.all(Radius.circular(25.0)))),
  54. ),
  55. ),
  56. Expanded(
  57. child: StreamBuilder(
  58. stream: _streamController.stream,
  59. builder: (context, snapshots) {
  60. if (snapshots.hasData) {
  61. return ListView.builder(
  62. itemCount: problemlist.length,
  63. itemBuilder: ((context, index) {
  64. problemModel problem = problemlist[index];
  65. return Card(
  66. margin: EdgeInsets.all(10),
  67. child: ListTile(
  68. title: Text(
  69. problem.name_surname,
  70. style: TextStyle(
  71. fontWeight: FontWeight.bold, fontSize: 19),
  72. ),
  73. subtitle: Text(
  74. problem.area,
  75. style: TextStyle(
  76. fontWeight: FontWeight.bold, fontSize: 17),
  77. ),
  78. trailing: Text(
  79. problem.status,
  80. style: TextStyle(
  81. fontWeight: FontWeight.bold, fontSize: 16),
  82. ),
  83. ),
  84. );
  85. }));
  86. }
  87. return Center(
  88. child: CircularProgressIndicator(),
  89. );
  90. },
  91. ),
  92. )
  93. ],
  94. ),
  95. ),
  96. );
  97. }
  98. }

答案1

得分: 0

  1. 你有一个定时器每秒定期运行,永远获取所有问题,这会覆盖任何过滤器。我认为你不想要那个定时器,只需在initState中调用一次getAllProblem()。然后将originalList保存在另一个变量中。类似这样
  2. class _view_problemState extends State&lt;view_problem&gt; {
  3. TextEditingController _searchController = TextEditingController();
  4. List&lt;problemModel&gt; problemlist = [];
  5. List&lt;problemModel&gt; originalList = [];
  6. StreamController _streamController = StreamController();
  7. Future getAllProblem() async {
  8. problemlist = await problemcontrollers().getProblem();
  9. originalList = problemlist;
  10. _streamController.sink.add(problemlist);
  11. }
  12. @override
  13. void initState() {
  14. getAllProblem();
  15. super.initState();
  16. }
  17. void _filterSearch(String query) {
  18. List&lt;problemModel&gt; filteredList = [];
  19. for (var item in originalList) {
  20. if (item.toString().contains(query.toString())) {
  21. filteredList.add(item);
  22. }
  23. }
  24. setState(() {
  25. problemlist = filteredList;
  26. });
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(
  32. backgroundColor: Color.fromARGB(255, 14, 12, 134),
  33. title: const Text('show information'),
  34. ),
  35. body: Container(
  36. child: Column(
  37. children: &lt;Widget&gt;[
  38. Padding(
  39. padding: const EdgeInsets.all(8.0),
  40. child: TextField(
  41. onChanged: (value) {
  42. _filterSearch(value);
  43. },
  44. controller: _searchController,
  45. decoration: InputDecoration(
  46. labelText: "search",
  47. prefixIcon: Icon(Icons.search),
  48. border: OutlineInputBorder(
  49. borderRadius: BorderRadius.all(Radius.circular(25.0)))),
  50. ),
  51. ),
  52. Expanded(
  53. child: StreamBuilder(
  54. stream: _streamController.stream,
  55. builder: (context, snapshots) {
  56. if (snapshots.hasData) {
  57. return ListView.builder(
  58. itemCount: problemlist.length,
  59. itemBuilder: ((context, index) {
  60. problemModel problem = problemlist[index];
  61. return Card(
  62. margin: EdgeInsets.all(10),
  63. child: ListTile(
  64. title: Text(
  65. problem.name_surname,
  66. style: TextStyle(
  67. fontWeight: FontWeight.bold, fontSize: 19),
  68. ),
  69. subtitle: Text(
  70. problem.area,
  71. style: TextStyle(
  72. fontWeight: FontWeight.bold, fontSize: 17),
  73. ),
  74. trailing: Text(
  75. problem.status,
  76. style: TextStyle(
  77. fontWeight: FontWeight.bold, fontSize: 16),
  78. ),
  79. ),
  80. );
  81. }));
  82. }
  83. return Center(
  84. child: CircularProgressIndicator(),
  85. );
  86. },
  87. ),
  88. )
  89. ],
  90. ),
  91. ),
  92. );
  93. }
  94. }
英文:

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

  1. class _view_problemState extends State&lt;view_problem&gt; {
  2. TextEditingController _searchController = TextEditingController();
  3. List&lt;problemModel&gt; problemlist = [];
  4. List&lt;problemModel&gt; originalList = [];
  5. StreamController _streamController = StreamController();
  6. Future getAllProblem() async {
  7. problemlist = await problemcontrollers().getProblem();
  8. originalList = problemlist;
  9. _streamController.sink.add(problemlist);
  10. }
  11. @override
  12. void initState() {
  13. getAllProblem();
  14. super.initState();
  15. }
  16. void _filterSearch(String query) {
  17. List&lt;problemModel&gt; filteredList = [];
  18. for (var item in originalList) {
  19. if (item.toString().contains(query.toString())) {
  20. filteredList.add(item);
  21. }
  22. }
  23. setState(() {
  24. problemlist = filteredList;
  25. });
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: AppBar(
  31. backgroundColor: Color.fromARGB(255, 14, 12, 134),
  32. title: const Text(&#39;show information&#39;),
  33. ),
  34. body: Container(
  35. child: Column(
  36. children: &lt;Widget&gt;[
  37. Padding(
  38. padding: const EdgeInsets.all(8.0),
  39. child: TextField(
  40. onChanged: (value) {
  41. _filterSearch(value);
  42. },
  43. controller: _searchController,
  44. decoration: InputDecoration(
  45. labelText: &quot;search&quot;,
  46. prefixIcon: Icon(Icons.search),
  47. border: OutlineInputBorder(
  48. borderRadius: BorderRadius.all(Radius.circular(25.0)))),
  49. ),
  50. ),
  51. Expanded(
  52. child: StreamBuilder(
  53. stream: _streamController.stream,
  54. builder: (context, snapshots) {
  55. if (snapshots.hasData) {
  56. return ListView.builder(
  57. itemCount: problemlist.length,
  58. itemBuilder: ((context, index) {
  59. problemModel problem = problemlist[index];
  60. return Card(
  61. margin: EdgeInsets.all(10),
  62. child: ListTile(
  63. title: Text(
  64. problem.name_surname,
  65. style: TextStyle(
  66. fontWeight: FontWeight.bold, fontSize: 19),
  67. ),
  68. subtitle: Text(
  69. problem.area,
  70. style: TextStyle(
  71. fontWeight: FontWeight.bold, fontSize: 17),
  72. ),
  73. trailing: Text(
  74. problem.status,
  75. style: TextStyle(
  76. fontWeight: FontWeight.bold, fontSize: 16),
  77. ),
  78. ),
  79. );
  80. }));
  81. }
  82. return Center(
  83. child: CircularProgressIndicator(),
  84. );
  85. },
  86. ),
  87. )
  88. ],
  89. ),
  90. ),
  91. );
  92. }
  93. }
  94. </details>

huangapple
  • 本文由 发表于 2023年7月3日 21:17:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定