【问题标题】:ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:RangeError(索引):无效值:有效值范围为空:0
【发布时间】:2021-04-09 03:30:49
【问题描述】:

这是我在 AssistantMethods.dart 中的代码

class AssistantMethods
{
  static Future<String> searchCoordinateAddress(Position position, context) async
  {
    String placeAddress = "";
    String st1, st2, st3, st4;
    String url = "https://maps.googleapis.com/maps/api/geocode/json? 
latlng=${position.latitude},${position.longitude}&key=$mapKey";
    print(position.latitude);
    print(position.longitude);

    var response = await RequestAssistant.getRequest(url);
    print(response);
    if(response != "failed")
    {
      //placeAddress = response["results"][0]["formatted_address"];
      st1 = placeAddress = response["results"][0]["address_components"][0]["long_name"];
      st2 = placeAddress = response["results"][0]["address_components"][1]["long_name"];
      st3 = placeAddress = response["results"][0]["address_components"][2]["long_name"];
      st4 = placeAddress = response["results"][0]["address_components"][3]["long_name"];
      placeAddress = st1 + ", " + st2 + ", " + st3 + ", " + st4;

      Address userPickUpAddress = new Address();
      userPickUpAddress.longitude = position.longitude;
      userPickUpAddress.latitude = position.latitude;
      userPickUpAddress.placeName = placeAddress;

      Provider.of<AppData>(context, listen: false).updatePickUpLocationAddress(userPickUpAddress);
    }

    return placeAddress;
  }

  static Future<DirectionDetails> obtainPlaceDirectionDetails(LatLng initialPosition, LatLng 
finalPosition) async
  {
    String directionUrl = "https://maps.googleapis.com/maps/api/directions/json? 

origin=${initialPosition.latitude},${initialPosition.longitude}&destination=${finalPosition.latitude}, ${finalPosition.longitude}&key=$mapKey";

    var res = await RequestAssistant.getRequest(directionUrl);

    if(res == "failed")
    {
      return null;
    }

    DirectionDetails directionDetails = DirectionDetails();

    directionDetails.encodedPoints = res["routes"][0]["overview_polyline"]["points"];

    directionDetails.distanceText = res["routes"][0]["legs"][0]["distance"]["text"];
    directionDetails.distanceValue = res["routes"][0]["legs"][0]["distance"]["value"];

    directionDetails.durationText = res["routes"][0]["legs"][0]["duration"]["text"];
    directionDetails.durationValue = res["routes"][0]["legs"][0]["duration"]["value"];

    return directionDetails;

  }
}

这是我在 MainScreen.dart 中的代码:

  Future<void> getPlaceDirection() async
  {
    var initialPos = Provider.of<AppData>(context, listen: false).pickUpLocation;
    var finalPos = Provider.of<AppData>(context, listen: false).dropOffLocation;

    var pickUpLatLng = LatLng(initialPos.latitude, initialPos.longitude);
    var dropOffLatLng = LatLng(finalPos.latitude, finalPos.longitude);

    showDialog(
        context: context,
        builder: (BuildContext context) => ProgressDialog(message: "Please wait...",)
    );

    var details = await AssistantMethods.obtainPlaceDirectionDetails(pickUpLatLng, dropOffLatLng);

    Navigator.pop(context);

    print("This is Encoded Points ::");
    print(details.encodedPoints);
  }
}

这是我的错误日志的一部分: ..................................................... ...................... I/flutter (26872):这是您的地址 ::Guillamac's Bldg., Carlos P. Garcia Avenue, 塔比拉兰市, 薄荷岛 W/IInputConnectionWrapper(26872):beginBatchEdit 处于非活动状态 输入连接 W/IInputConnectionWrapper(26872):getTextBeforeCursor 处于非活动状态 输入连接 W/IInputConnectionWrapper(26872):getTextAfterCursor 处于非活动状态
输入连接 W/IInputConnectionWrapper(26872):非活动 InputConnection 上的 getSelectedText W/IInputConnectionWrapper(26872):非活动 InputConnection 上的 endBatchEdit W/IInputConnectionWrapper(26872):非活动 InputConnection 上的 beginBatchEdit W/IInputConnectionWrapper(26872):非活动 InputConnection 上的 endBatchEdit I/Choreographer(26872):跳过了 36 帧!该应用程序可能也在做 主要的工作量很大 线。 W/Looper (26872):慢 Looper main:doFrame 由于 1 msg 延迟了 614 毫秒 I/flutter (26872): 这是下车地点:: I/flutter (26872): BQ商城 W/Looper (26872):慢 Looper main:doFrame 由于 1 msg 延迟了 472 毫秒 E/flutter (26872): [错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理 例外:范围错误 (index):无效值:有效值范围为空:0 E/flutter (26872): #0 List.[] (dart:core- 补丁/growable_array.dart:254:60) E/flutter (26872):#1 AssistantMethods.obtainPlaceDirectionDetails (包:rider_app/Assistants/assistantMethods.dart:58:51) E/颤动​​(26872): E/flutter (26872): #2 _MainScreenState.getPlaceDirection enter code here(package:rider_app/AllScreens/mainscreen.dart:294:19) E/颤动​​(26872): E/颤振(26872):#3 _MainScreenState.build。 (包:rider_app/AllScreens/mainscreen.dart:202:27) E/颤动​​(26872): E/颤振(26872):

【问题讨论】:

    标签: flutter maps google-places-api


    【解决方案1】:

    这个错误表示,你正在调用那些不存在的索引,这里你的错误清楚地表明你的列表是空的,并且你在它的索引处调用值,你应该在调用它的索引之前检查列表长度。

    我不知道您在哪个列表中收到此错误。 但回答我从您的代码中找出的所有 4 个列表。

    
    print(response);
       if(response != "failed")
       {
         var length:response["results"].length;
         print("NUMBERS OF RESULTS:$length");
         if(length > 0){ //check length for result list
            if( response["results"][0]["address_components"] > 0){ //this for address_components list 
          //Perform your code here
    
              
    
             }
          
          }
           
     
          }
       
    
    

    var length:res["routes"].length;
    print("NUMBERS OF ROUTES:$length");
    
    if(length > 0){ //check length for route list
      if(res["routes"][0]["legs"].length > 0){ //check length for legs list
          //perform your code here
       }
     
     }
    
    

    【讨论】:

    • 我已经找到了解决办法,我必须禁用apiKey限制,在tge google maps platform api key limits的设置中更改为无限制。
    • 如何做到这一点@NinYu ...禁用方向api的api键限制?
    • @Ali Yar Khan 是的!
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2021-07-31
    • 2020-06-06
    • 2021-10-06
    • 2021-11-13
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多