首页 > 解决方案 > 在颤动中打开位置后如何更新我的位置

问题描述

我是 Flutter 的新手,我想在这个例子中实现一些目标,我想在用户打开位置后更新用户位置,假设用户在我们给用户一个弹出窗口说这个应用程序之后没有先打开他的位置需要位置然后它应该更新数据但在下面的示例中它不起作用,请帮助我。

这是我正在处理的示例

PS:

在此处输入图像描述

标签: androiddartflutter

解决方案


只需像示例中一样订阅“onLocationChanged”流。

_location.onLocationChanged().listen((Map<String,double> result) {
          var latitude = result["latitude"]; //This is called always when the location updates
          var longitude = result["longitude"];
        });

要在用户未启用位置时显示弹出窗口,请使用以下命令:

try {
  currentLocation = await location.getLocation;
} on PlatformException {
await showDialog<dynamic>(
              context: context,
              builder: (context) {
                return AlertDialog(
                  title: Text("No Location"),
                  content: Text(
                      "Please allow this App to use Location or turn on your GPS."),
                  actions: <Widget>[
                    FlatButton(
                      child: Text(
                        "Ok"
                      ),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                );
              });
}

推荐阅读