Have recently installed Flutter null safety
and having fixed most of the code I am having trouble with an API call.
It returns _TypeError (type 'Null' is not a subtype of type 'FutureOr<Stations>')
I haven't found a SO answer that seems to line up with my issue/ code method.
This is the button click that calls the API from main.dart
void _anchoredMapMarkersButtonClicked() {
_mapMarkerExample.showAnchoredMapMarkers();
}
And this is the API call itself with error displaying at the bottom } catch(Exception) {
line
Future<Stations> fetchStations() async {
var client = http.Client();
var 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(Exception) {
return stations;
}
return stations;
}
Updated Code
Future<Stations> fetchStations() async {
var client = http.Client();
Stations? stations;
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
var lat = position.latitude;
var long = position.longitude;
try{
var response = await client.get(Uri.parse('https://transit.hereapi.com/v8/stations?in=$lat,$long&return=transport&apiKey=uIioesb_EbnEXgPHlH0Z5e7gPGy9irpq2dRJcjuYPZY'));
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
//print(jsonMap);
inspect(jsonMap);
stations = Stations.fromJson(jsonMap);
//print(stations);
}
} catch(Exception) {
Future<Stations?> fetchStations()
}
Future<Stations?> fetchStations()
}