Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何关闭具有过时上下文的SearchDelegate?_Flutter - Fatal编程技术网

Flutter 如何关闭具有过时上下文的SearchDelegate?

Flutter 如何关闭具有过时上下文的SearchDelegate?,flutter,Flutter,我已经为我的位置搜索代理建立了一个建议列表,该列表的顶部总是有一个项目,可以将用户推到新页面。在这个新页面上,用户可以在地图上选择一个位置并提交它或返回到搜索页面。如果用户提交了位置,我希望以所选位置作为结果关闭基础位置搜索代理 对于这种行为,我在我的无状态建议列表小部件中使用回调,但在onMapTapped回调的情况下,应用程序会抛出一个异常: class LocationSearchDelegate extends SearchDelegate<Location> { //

我已经为我的位置搜索代理建立了一个建议列表,该列表的顶部总是有一个项目,可以将用户推到新页面。在这个新页面上,用户可以在地图上选择一个位置并提交它或返回到搜索页面。如果用户提交了位置,我希望以所选位置作为结果关闭基础位置搜索代理

对于这种行为,我在我的无状态建议列表小部件中使用回调,但在onMapTapped回调的情况下,应用程序会抛出一个异常:

class LocationSearchDelegate extends SearchDelegate<Location> {
  // ...
  @override
  Widget buildSuggestions(BuildContext context) {
    return _SuggestionList(
      query: query,
      onSelected: (Location suggestion) {
        result = suggestion;
        close(context, result);
      },
      onMapTapped: (Location location) {
        result = location;
        close(context, result); // <- Throws exception
      },
    );
  }
  // ...
}
同时,
buildSuggestions
方法的上下文由于导航到
ChooseOnMapPage
,而变得过时:

class _SuggestionList extends StatelessWidget {
  // ...
  void _handleOnChooseOnMapTap(BuildContext context) async {
    Location location = await Navigator.of(context).push(
      MaterialPageRoute<Location>(builder: (context) {
        return ChooseLocationPage();
      }),
    );
    onMapTapped(location);
  }
  // ...
}
class\u SuggestionList扩展了无状态小部件{
// ...
void\u handleOnChooseOnMapTap(BuildContext上下文)异步{
Location Location=wait Navigator.of(context.push)(
MaterialPage路线(生成器:(上下文){
返回ChooseLocationPage();
}),
);
位置;
}
// ...
}
解决方法

当前的解决方法是显示结果,然后立即关闭搜索代理:

class LocationSearchDelegate extends SearchDelegate<Location> {
  // ...
  @override
  Widget buildSuggestions(BuildContext context) {
    return _SuggestionList(
      query: query,
      onSelected: (Location suggestion) {
        result = suggestion;
        close(context, result);
      },
      onMapTapped: (Location location) {
        result = location;
        showResults(context); // <- Throws NO exception
      },
    );
  }
  // ...
  @override
  Widget buildResults(BuildContext context) {
    Future.delayed(Duration.zero, () {
      close(context, result);
    });
    return Container();
  }
  // ...
}
class LocationSearchDelegate扩展了SearchDelegate{
// ...
@凌驾
小部件构建建议(构建上下文){
返回建议列表(
查询:查询,
onSelected:(位置建议){
结果=建议;
关闭(上下文、结果);
},
onMapTapped:(位置){
结果=位置;
展示结果(背景)//
class LocationSearchDelegate extends SearchDelegate<Location> {
  // ...
  @override
  Widget buildSuggestions(BuildContext context) {
    return _SuggestionList(
      query: query,
      onSelected: (Location suggestion) {
        result = suggestion;
        close(context, result);
      },
      onMapTapped: (Location location) {
        result = location;
        showResults(context); // <- Throws NO exception
      },
    );
  }
  // ...
  @override
  Widget buildResults(BuildContext context) {
    Future.delayed(Duration.zero, () {
      close(context, result);
    });
    return Container();
  }
  // ...
}