Failed Assertion: Line 20 Pos 16: 'longitude != Null': Is Not True
I'm trying to build a TaxiApp where a passenger is supposed to search for a dropOff location but each time the user searches for a location and clicks on it using the Google Autocomplete It keeps bringing this error: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: 'package:google_maps_flutter_platform_interface/src/types/location.dart': Failed assertion: line 20 pos 16: 'longitude != null': is not true.
That error points to this method
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);
});
}
Answer
Understanding The Problem
You need to use FutureBuilder
widget because your App depends on on waiting for another operation to complete. Otherwise latitude value will be NULL.
Check this - https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
Solution
You must wrap your body of Scaffold (where you are actually calling the main map widget) with FutureBuilder
. You haven't added the main function from where you are calling your map widget but the following is the pseudo-code to do it -
@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
}
}
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?