这是我在 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 on inactive
输入连接
W/IInputConnectionWrapper(26872): getTextAfterCursor on inactive
输入连接
W/IInputConnectionWrapper(26872):非活动输入连接上的 getSelectedText
W/IInputConnectionWrapper(26872): 非活动 InputConnection 上的 endBatchEdit
W/IInputConnectionWrapper(26872): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(26872): 非活动 InputConnection 上的 endBatchEdit
I/Choreographer(26872):跳过36帧!应用程序可能也在做
在其主要工作上做了很多工作
线。
W/Looper (26872):Slow Looper main:由于 1 msg,doFrame 延迟了 614 毫秒
I/flutter (26872):这是下车地点::
I/扑(26872):BQ商城
W/Looper (26872):Slow Looper main:由于 1 msg,doFrame 延迟了 472 毫秒
E/flutter (26872): [ERROR: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/flutter (26872):#3 _MainScreenState.build。
(包:rider_app/AllScreens/mainscreen.dart:202:27)
E/扑(26872):
E/扑(26872):
答案 0 :(得分:0)
这个错误表示,你正在调用那些不存在的索引,这里你的错误清楚地表明你的列表是空的,并且你在它的索引处调用值,你应该在调用它的索引之前检查列表长度。
我不知道您在哪个列表中收到此错误。 但是回答了我从您的代码中得出的所有 4 个列表。
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
}
}
或
{{1}}