未定义名称 ‘duplicateItems’。尝试更正名称为已定义的名称,或定义该名称。

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

Undefined name 'duplicateItems'. Try correcting the name to one that is defined, or defining the name

问题

未定义名称'duplicateItems'。
尝试更正名称为已定义的名称,或定义该名称。
我已将其更正为另一个名称,仍然遇到相同的问题。

class view_problem extends StatefulWidget {
  @override
  State<view_problem> createState() => _view_problemState();
}

class _view_problemState extends State<view_problem> {
  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) {
    setState(() {
      problemlist = duplicateItems
          .where((item) => item.toLowerCase().contains(query.toLowerCase()))
          .toList();
    });
  }
}
英文:

Undefined name 'duplicateItems'.
Try correcting the name to one that is defined, or defining the name.
I have fixed it to another one still stuck with the same problem.

class view_problem extends StatefulWidget {
  @override
  State&lt;view_problem&gt; createState() =&gt; _view_problemState();
}

class _view_problemState extends State&lt;view_problem&gt; {
  List&lt;problemModel&gt; 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) {
    setState(() {
      problemlist = duplicateItems
          .where((item) =&gt; item.toLowerCase().contains(query.toLowerCase()))
          .toList();
    });
  }

答案1

得分: 0

你没有在你的函数或全局变量中定义duplicateItems。在使用之前,你必须先定义它。无论duplicateItems是一个字符串、整数还是其他类型的代码都不知道,所以我们必须明确定义它。在你的情况下,我猜它是一个字符串列表,因为它是一个搜索函数,所以我的答案是相应的。

更新的答案:在这里,我还进行了一项更改,以便在搜索查询中有空格时,它仍然会给你一个有效的结果,如果这有帮助,请接受这个答案并点赞!!

void _filterSearch(String query) {
   if (query.isNotEmpty) {
      List&lt;ApptUsersModel&gt; dummyListData = [];
      _dummySearchList.forEach((element) {
        var fullName = (element.firstname!+element.lastname!).trim();
        if (fullName.toLowerCase().contains(query.toLowerCase().replaceAll(&#39; &#39;, &#39;&#39;).trim())) {
          dummyListData.add(element);
        }
      });
      Utility.log(&quot;Length of dummyListData is --&gt; ${dummyListData.length}&quot;);

      setState(() {
        _searchResultList.clear();
        _searchResultList.addAll(dummyListData);
        Utility.log(
            &quot;Length of _searchResultList in if is --&gt; ${_searchResultList.length}&quot;);
      });
      return;
    }
  }
英文:

You have not defined duplicateItems in your function or even global variable. You have to define it before using it. Weather duplicateItems is a String, int or some other type code doesn't know so we have to mention define it.In your case it is a list of String I guess as it is a search function so my answer is accordingly.

UPDATED Answer:- Here I have also made a change so that when there is space in the search query it will still give you a valid result and if it help please accept this answer and give an upvote !!

void _filterSearch(String query) {
   if (query.isNotEmpty) {
      List&lt;ApptUsersModel&gt; dummyListData = [];
      _dummySearchList.forEach((element) {
        var fullName = (element.firstname!+element.lastname!).trim();
        if (fullName.toLowerCase().contains(query.toLowerCase().replaceAll(&#39; &#39;, &#39;&#39;).trim())) {
          dummyListData.add(element);
        }
      });
      Utility.log(&quot;Length of dummyListData is --&gt; ${dummyListData.length}&quot;);

      setState(() {
        _searchResultList.clear();
        _searchResultList.addAll(dummyListData);
        Utility.log(
            &quot;Length of _searchResultList in if is --&gt; ${_searchResultList.length}&quot;);
      });
      return;
    }
  }


</details>



huangapple
  • 本文由 发表于 2023年6月22日 10:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528261.html
匿名

发表评论

匿名网友

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

确定