错误:fltter/lib/ui/ui_dart_state.cc(186)]未处理的异常:RangeError(Index):无效值:有效值范围为空:0 [英] ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0

查看:19
本文介绍了错误:fltter/lib/ui/ui_dart_state.cc(186)]未处理的异常:RangeError(Index):无效值:有效值范围为空:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在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? 
原点=${InitialPosition.atitude},${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/Ffltter(26872):这是你的地址::Guillamac‘s Bldg,Carlos P. 博霍市塔比拉兰市加西亚大道 W/I输入连接包装(26872):不活动时开始批处理编辑 输入连接 W/I输入连接包装(26872):非活动时获取文本优先于光标 输入连接 W/I输入连接包装(26872):非活动状态下的GetTextAfterCursor
输入连接 W/I输入连接包装(26872):非活动输入连接上的GetSelectedText W/I输入连接包装(26872):非活动输入连接上的结束批处理编辑 W/I输入连接包装(26872):非活动输入连接上的开始批处理编辑 W/I输入连接包装(26872):非活动输入连接上的结束批处理编辑 I/编舞(26872):跳过36帧!应用程序可能也在执行此操作 在它的主要方面做了很多工作 线。 W/Looper(26872):慢循环Main:由于1条消息,doFrame延迟614毫秒 I/颤动(26872):这是放行位置:: 《I/Fighter》(26872):BQ Mall W/Looper(26872):慢循环Main:由于1条消息,doFrame延迟472毫秒 E/Ffltter(26872):[错误:Ffltter/lib/ui/ui_dart_state.cc(186)]未处理 异常:RangeError (索引):无效值:有效值范围为空:0 E/颤动(26872):#0列表。[](飞镖:核心- Patch/Growable_array.Dart:254:60) 电子/颤动(26872):排名第一的辅助方法。获取位置方向详细信息 (package:rider_app/Assistants/assistantMethods.dart:58:51) 电子/颤动(26872): E/颤动(26872):#2_主屏幕状态。getPlaceDirectionenter code here(package:rider_app/AllScreens/mainscreen.dart:294:19) 电子/颤动(26872): 电子颤动(26872):#3_MainScreenState.Build。 (package:rider_app/AllScreens/mainscreen.dart:202:27) 电子/颤动(26872): 电子/颤动(26872):

推荐答案

此错误表示您正在调用那些不存在的索引,此处您的错误清楚地表明您的列表为空,并且您正在调用其索引处的值,您应该在调用其索引之前检查列表长度。

我不知道您在哪个列表中收到此错误。 而是回答我从你的代码中得出的所有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
   }
 
 }

这篇关于错误:fltter/lib/ui/ui_dart_state.cc(186)]未处理的异常:RangeError(Index):无效值:有效值范围为空:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆