Flutter 我怎样才能找到正确的索引?

Flutter 我怎样才能找到正确的索引?,flutter,dart,Flutter,Dart,搜索时,它会根据查询列表的索引进行。所以它转到了错误的页面。我希望列表按照它自己的索引进行。如何获得正确的索引并将其应用于此处: Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(index))); 在SearchDelegate下生成建议: @override Widget buildSuggestions(BuildContext context) {

搜索时,它会根据查询列表的索引进行。所以它转到了错误的页面。我希望列表按照它自己的索引进行。如何获得正确的索引并将其应用于此处:

Navigator.push(context,
              MaterialPageRoute(builder: (context) => DetailPage(index)));
SearchDelegate
下生成建议:

@override
  Widget buildSuggestions(BuildContext context) {
    final suggestionList = query.isEmpty
        ? [" "]
        : MyData.TEAM_NAME
            .where((p) => p.toLowerCase().contains(query.toLowerCase()))
            .toList();

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
        onTap: () {
          close(context, suggestionList[index]);
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => DetailPage(index)));
        },
        title: Text(suggestionList[index]),
      ),
      itemCount: suggestionList.length,
    );
  }
我的数据列表:

class MyData {
  static const List<String> TEAM_NAME = [
    "team1 name",
    "team2 name",
    "abc Team",
    "xyz"
  ];

  static const List<String> INFO1 = [
    "team 1 first info",
    "team2 info1",
    "abc's info",
    "xyz's info1"
  ];
  static const List<String> INFO2 = [
    "team 1 second info",
    "team2 info2",
    "abc's info 2",
    "xyz's info2"
  ];
}

MyData.TEAM_NAME.where((p)=>p.toLowerCase().contains(query.toLowerCase()).toList(),这就是标记不对应的原因

您可以这样更改代码:

@override
  Widget buildSuggestions(BuildContext context) {
    final suggestionList = query.isEmpty
        ? [" "]
        : MyData.TEAM_NAME
            .where((p) => p.toLowerCase().contains(query.toLowerCase()))
            .toList();

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
        onTap: () {
          close(context, suggestionList[index]);
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => DetailPage( MyData.TEAM_NAME.indexWhere((item)=> item == suggestionList[index]) )));
        },
        title: Text(suggestionList[index]),
      ),
      itemCount: suggestionList.length,
    );
  }

看了你的视频链接后我发现, 您正在将索引发送到下一页(而不是项目Id或名称)

意味着

为了更好地理解, 我就是这样,

该列表首次显示了4个项目。 你点击第三项,点击右键后显示数据,因为你发送了第三个索引, 在详细页面中,您还可以从第三个索引中获取数据并显示文本

但在搜索项示例(xyz..)中,您的数据显示在第一个索引中。然后单击并将索引发送到下一页,它将显示第一个位置索引。因此,它将显示团队1的名称数据

简而言之,您在DetailPage中发送索引,这是搜索中的一个问题。 所以,为了获得正确的方法,请为列表项分配Id

@override
  Widget buildSuggestions(BuildContext context) {
    final suggestionList = query.isEmpty
        ? [" "]
        : MyData.TEAM_NAME
            .where((p) => p.toLowerCase().contains(query.toLowerCase()))
            .toList();

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
        onTap: () {
          close(context, suggestionList[index]);
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => DetailPage( MyData.TEAM_NAME.indexWhere((item)=> item == suggestionList[index]) )));
        },
        title: Text(suggestionList[index]),
      ),
      itemCount: suggestionList.length,
    );
  }
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(index))); },