断言失败:第 20 行第 16 行:'longitude != null':不正确
Failed assertion: line 20 pos 16: 'longitude != null': is not true
我正在尝试构建一个 TaxiApp,乘客应该在其中搜索下车位置,但每次用户搜索位置并使用 Google 自动完成点击它时 它不断带来此错误:[错误: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);
});
}
理解问题
您需要使用 FutureBuilder
小部件,因为您的应用程序依赖于等待另一个操作完成。否则纬度值将为 NULL.
检查这个 - https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
解决方案
您必须用 FutureBuilder
包装您的脚手架主体(您实际调用主地图小部件的地方)。您尚未从调用地图小部件的位置添加主要功能,但以下是执行此操作的伪代码 -
@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
}
}
我正在尝试构建一个 TaxiApp,乘客应该在其中搜索下车位置,但每次用户搜索位置并使用 Google 自动完成点击它时 它不断带来此错误:[错误: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);
});
}
理解问题
您需要使用 FutureBuilder
小部件,因为您的应用程序依赖于等待另一个操作完成。否则纬度值将为 NULL.
检查这个 - https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
解决方案
您必须用 FutureBuilder
包装您的脚手架主体(您实际调用主地图小部件的地方)。您尚未从调用地图小部件的位置添加主要功能,但以下是执行此操作的伪代码 -
@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
}
}