I have recently transferred a fully functioning app to dart null safety
and it has thrown some errors
This API call has been adapted but now I am receiving the error flutter: Exception Happened: type 'String' is not a subtype of type 'Mode'
I call the API by a function which gains the user position to make the call.
To reiterate, this was working perfectly before upgrading dart so it's an issue of adapting to null safety I believe.
The API call (Where the exception is throwing)
Future<Stations?> fetchStations() async {
var client = http.Client();
Stations? stations;
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var lat = position.latitude;
var long = position.longitude;
try{
var response = await client.get(Uri.parse('https_call'));
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
stations = Stations.fromJson(jsonMap);
//print(stations);
}
} catch(e) {
print("Exception Happened: ${e.toString()}");
}
return stations;
}
The function to call API
Future<void> showAnchoredMapMarkers() async {
var stations = await fetchStations();
for (Station stations in stations!.stations) {
GeoCoordinates geoCoordinates = GeoCoordinates (stations.place.location.lat, stations.place.location.lng);
var id = stations.place.id;
_addCircleMapMarker(geoCoordinates, 0);
_addPOIMapMarker(geoCoordinates, 1);
}
}
Update
class Station {
Station({
this.place,
required this.transports,
});
Place place;
List<Transport> transports;
factory Station.fromJson(Map<String, dynamic> json) => Station(
place: json["place"] == null ? null : Place.fromJson(json["place"]),
transports: json["transports"] == null ? null : List<Transport>.from(json["transports"].map((x) => Transport.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"place": place == null ? null : place.toJson(),
"transports": transports == null ? null : List<dynamic>.from(transports.map((x) => x.toJson())),
};
}
The lines such as
place: json["place"] == null ? null : Place.fromJson(json["place"]),
are all red underlined with errors such as
The argument type 'Place?' can't be assigned to the parameter type 'Place'.