【问题标题】:Exception: RangeError (index): Invalid value: Valid value range is empty: 0异常: RangeError(索引):无效值:有效值范围为空:0
【发布时间】:2020-06-06 11:30:23
【问题描述】:

我正在尝试在地图上的两个位置之间创建路径,并且我在正确的位置上获得了标记,但由于此错误而未绘制路径。

setPolylines() async 
{   
  List<PointLatLng> result = await
    polylinePoints.getRouteBetweenCoordinates(
      googleAPIKey,
      SOURCE_LOCATION.latitude, 
      SOURCE_LOCATION.longitude,
      DEST_LOCATION.latitude, 
      DEST_LOCATION.longitude
    );   

    if(result.isNotEmpty){      
      result.forEach((PointLatLng point){
        polylineCoordinates.add(
          LatLng(point.latitude, point.longitude));
      });
    }

    setState(() {
      Polyline polyline = Polyline(
        polylineId: PolylineId("poly"),
        color: Color.fromARGB(255, 40, 122, 198),
        points: polylineCoordinates
      );
      _polylines.add(polyline);    
    });
}

未处理的异常:异常:RangeError(索引):无效值:有效值范围为空:0

我该如何解决这个问题?

【问题讨论】:

  • 你找到解决办法了吗?

标签: list google-maps flutter dart google-polyline


【解决方案1】:

该错误可能是因为未在您的 Google 控制台上启用 Directions API,在我的情况下,我还必须在启用它以显示路线后设置我的帐单。您还可以使用 300 美元的免费试用额度。

【讨论】:

    【解决方案2】:

    您收到此错误是因为{ "error_message": "This API project is not authorized to use this API.", "routes": [], "status": "REQUEST_DENIED" }

    确保启用地图 api。

    【讨论】:

    • 你怎么知道他收到了这个错误?当他什么地方都没提!!!
    【解决方案3】:

    试试这个。

     await
        polylinePoints.getRouteBetweenCoordinates(
          googleAPIKey,
          SOURCE_LOCATION.latitude, 
          SOURCE_LOCATION.longitude,
          DEST_LOCATION.latitude, 
          DEST_LOCATION.longitude
        ).then((result){
    
    if(result.isNotEmpty){      
      result.forEach((PointLatLng point){
        polylineCoordinates.add(
          LatLng(point.latitude, point.longitude));
      });
    }
    
    setState(() {
      Polyline polyline = Polyline(
        polylineId: PolylineId("poly"),
        color: Color.fromARGB(255, 40, 122, 198),
        points: polylineCoordinates
      );
      _polylines.add(polyline);    
    });
       });
    

    【讨论】:

    • 不,没有用。它抛出相同的异常。抱歉,回复晚了。
    • 可以添加 results.toString() 输出吗?
    • 是的,当然!!但是我应该在哪里添加 result.toString() ,因为它在polylinePoints.getRouteBetweenCoordinates 抛出异常,然后我添加了它然后它没有打印。
    • 你确定你创建对象成功(PolylinePoints polylinePoints = PolylinePoints();) 并放置正确的googleapikey?
    • 是的,我检查了这两件事,还检查了 Directions API 的仪表板,它显示命中但失败了
    猜你喜欢
    • 2021-06-16
    • 2019-07-25
    • 2023-03-16
    • 2021-10-06
    • 2021-11-13
    • 2020-10-07
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多