我正在尝试构建一个出租车应用程序,乘客应该在其中搜索下车位置,但每次用户搜索一个位置并使用 Google 自动完成点击它时,它都会不断带来这个错误:[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:'package:google_maps_flutter_platform_interface/src/types/location.dart':断言失败:第 20 行 pos 16:'longitude != null':不是真的。
那个错误指向这个方法
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 content) => ProgressDialog(message: "Please wait...",)
);
var details = await AssistantMethods.obtainPlaceDirectionDetails(pickUpLatLng, dropOffLatLng);
setState(() {
tripDirectionDetails = details;
});
Navigator.pop(context);
print("This is the encoded points:: ");
print(details.encodedPoints);
PolylinePoints polylinePoints = PolylinePoints();
List<PointLatLng> decodedPolyLinePointsResult = polylinePoints.decodePolyline(details.encodedPoints);
pLineCoordinates.clear();
if (decodedPolyLinePointsResult.isNotEmpty)
{
decodedPolyLinePointsResult.forEach((PointLatLng pointLatLng)
{
pLineCoordinates.add(LatLng(pointLatLng.latitude, pointLatLng.longitude));
});
}
polylineSet.clear();
setState(() {
Polyline polyline = Polyline(
color: orange,
polylineId: PolylineId("PolylineID"),
jointType: JointType.round,
points: pLineCoordinates,
width: 5,
startCap: Cap.roundCap,
endCap: Cap.roundCap,
geodesic: true,
);
polylineSet.add(polyline);
});
LatLngBounds latLngBounds;
if(pickUpLatLng.latitude > dropOffLatLng.latitude && pickUpLatLng.longitude > dropOffLatLng.longitude)
{
latLngBounds = LatLngBounds(southwest: dropOffLatLng, northeast: pickUpLatLng);
}
else if(pickUpLatLng.longitude> dropOffLatLng.longitude)
{
latLngBounds = LatLngBounds(southwest: LatLng(pickUpLatLng.latitude, dropOffLatLng.longitude),
northeast: LatLng(dropOffLatLng.latitude, pickUpLatLng.longitude));
}
else if(pickUpLatLng.latitude > dropOffLatLng.latitude)
{
latLngBounds = LatLngBounds(southwest: LatLng(dropOffLatLng.latitude, pickUpLatLng.longitude),
northeast: LatLng(pickUpLatLng.latitude, dropOffLatLng.longitude));
}
else
{
latLngBounds = LatLngBounds(southwest: pickUpLatLng, northeast: dropOffLatLng);
}
newGoogleMapController.animateCamera(CameraUpdate.newLatLngBounds(latLngBounds, 70));
Marker pickUpLocMarker = Marker(
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
infoWindow: InfoWindow(title: initialPos.placeName, snippet: "My location"),
position: pickUpLatLng,
markerId: MarkerId("pickUpId"),
);
Marker dropOffLocMarker = Marker(
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
infoWindow: InfoWindow(title: finalPos.placeName, snippet: "Drop-off location"),
position: dropOffLatLng,
markerId: MarkerId("dropOffId"),
);
setState(() {
markerSet.add(pickUpLocMarker);
markerSet.add(dropOffLocMarker);
});
Circle pickUpLocCircle = Circle(
fillColor: Colors.blue,
center: pickUpLatLng,
radius: 12,
strokeWidth: 4,
strokeColor: Colors.blueAccent,
circleId: CircleId("pickUpId")
);
Circle dropOffLocCircle = Circle(
fillColor: Colors.red,
center: dropOffLatLng,
radius: 12,
strokeWidth: 4,
strokeColor: Colors.redAccent,
circleId: CircleId("dropOffId"),
);
setState(() {
circleSet.add(pickUpLocCircle);
circleSet.add(dropOffLocCircle);
});
}
答案 0 :(得分:0)
了解问题
您需要使用 FutureBuilder
小部件,因为您的应用依赖于等待另一个操作完成。否则纬度值将为 NULL。
检查这个 - https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
解决方案
您必须用 FutureBuilder
包裹您的 Scaffold 主体(您实际调用主地图小部件的位置)。您尚未从调用地图小部件的位置添加 main 函数,但以下是执行此操作的伪代码 -
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white),
title: Text('Map'),
centerTitle: true,
backgroundColor: Colors.blue,
),
//this mapWidget should be wrapped in future builder
body: mapWidget(),
);
}
Widget mapWidget() {
return FutureBuilder(
future: getPlaceDirection,//adding your main location widget in future
builder: (context, state) {
if (state.connectionState == ConnectionState.active ||
state.connectionState == ConnectionState.waiting) {
return SpinKitRipple(
itemBuilder: (BuildContext context, int index) {
return DecoratedBox(
decoration: BoxDecoration(
color: index.isEven ? Colors.grey :
Color(0xffffb838),
),);},);
}
else {
//here you should write your main logic to be shown once latitude ! = NULL
}
}