【问题标题】:Flutter: Red Screen Appear when Calculation of Value from Future Function and Value from ArrayFlutter:从未来函数和数组中计算值时出现红屏
【发布时间】:2020-03-24 13:43:14
【问题描述】:

我正在尝试使用此公式计算两个位置之间的距离

double calculateDistance(lat1, lon1, lat2, lon2){
    var p = 0.017453292519943295;
    var c = cos;
    var a = 0.5 - c((lat2 - lat1) * p) / 2 +
            c(lat1 * p) * c(lat2 * p) *
            (1 - c((lon2 - lon1) * p)) / 2;
    return 12742 * asin(sqrt(a));
}

lat1lon1 的值来自 geolocator: 使用 getCurrentPosition,而

lat2lon2的值来自我制作的array class

但在几毫秒内出现错误,然后出现红屏因为在geolocator尝试获取经纬度时,lat1lon1的值为null,则 >“红屏”也会在 GPS 关闭时出现。


这是我的地理定位功能代码

class _HomePageState extends State<HomePage> {
  Geolocator geolocator = Geolocator();
  Position _currentUserPosition;
  var distance;

  @override
  void initState() {
    super.initState();
    _initCurrentUserLocation();
  }

  @override
  void didUpdateWidget(Widget oldWidget) {
    super.didUpdateWidget(oldWidget);
    setState(() {
      _currentUserPosition = null;
    });
  }

  _initCurrentUserLocation() async {
    geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best,
    ).then((position) {
    if (mounted) {
    setState(() =>_currentUserPosition = position);
    }
    }).catchError((e) {});
  }

这是我的小部件构建 使用 ListView Builder 从数组文件中获取值 lat2 和 lon2。

double calculateDistance(lat1, lon1, lat2, lon2){
    var p = 0.017453292519943295;
    var c = cos;
    var a = 0.5 - c((lat2 - lat1) * p) / 2 +
            c(lat1 * p) * c(lat2 * p) *
            (1 - c((lon2 - lon1) * p)) / 2;
    return 12742 * asin(sqrt(a));
}

@override
Widget build(BuildContext context) {
  final coordinateData = Data.dataArray;
  var distance;

  return ListView.builder(
    itemCount: dataCoordinate.length,
    itemBuilder: (context, index) {

      distance = calculateDistance(
          _currentPosition.latitude, _currentUserPosition.longitude,
          coordinateData[index].lat, coordianteData[index].long
      );

      return ListTile(
          title: Text(distance.toStingAsFixed(2)),
      );
    },
  );
}

我的要求是

lat1lon1仍为nulldistance的值为null /strong>,或者手机上的 GPS 已关闭

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
    getCurrentLocation() async {
        //Future because we dont know its future value
        try {
          Position position = await Geolocator()
              .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
          latitude = position.latitude;
          longitude = position.longitude;
        } catch (e) {
          print("PERMISSION Denied");
        }
    

    将此代码包装在 try catch 块中并使用 async await 来获取未来值 catch 块执行的条件: 1.权限被拒绝 2.获取不到位置

    如果出现以下任何情况,请在 catch 块中执行您想要的操作。您不需要 if else 条件,只需使用 try catch 和 async await bettr 即可获得 async await 教程,如果您不明白我的意思,只需谷歌即可。

    【讨论】:

      【解决方案2】:

      在你的函数中设置纬度和经度的默认值

      double calculateDistance({lat1=0, lon1=0, lat2=0, lon2=0}){
          var p = 0.017453292519943295;
          var c = cos;
          var a = 0.5 - c((lat2 - lat1) * p) / 2 +
                  c(lat1 * p) * c(lat2 * p) *
                  (1 - c((lon2 - lon1) * p)) / 2;
          return 12742 * asin(sqrt(a));
      }
      

      当调用你这样设置的函数时

      distance = calculateDistance(
                lat1:_currentPosition.latitude, 
                lon1:_currentUserPosition.longitude,
                lat2:coordinateData[index].lat, 
                lon2: coordianteData[index].long,
            );
      

      另一种方法是设置函数作为参数接收位置和验证是通过了 2

      double calculateDistance({Position position1, Position position2}){
          if (position1 == null || position2 == null) return 0;
          var lat1=position1.lat, lon1=position1.lon;
          var lat2=position2.lat, lon2=position2.lon;
          var p = 0.017453292519943295;
          var c = cos;
          var a = 0.5 - c((lat2 - lat1) * p) / 2 +
                  c(lat1 * p) * c(lat2 * p) *
                  (1 - c((lon2 - lon1) * p)) / 2;
          return 12742 * asin(sqrt(a));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-20
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 2021-05-14
        • 2017-07-09
        • 2022-09-24
        相关资源
        最近更新 更多