'Flutter MapBox - Unhandled Exception: type 'Null' is not a subtype of type
I am just following this guide https://github.com/Imperial-lord/mapbox-flutter, In turn by turn navigation, getting error like 'Unhandled Exception: type 'Null' is not a subtype of type 'String' - In emulator i am getting error like
Syncing files to device iPhone 13... flutter: flutter: Oops something went wrong [VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'String' #0 getParsedReverseGeocoding (package:t..helpers/mapbox_handler.dart:42:19) <asynchronous suspension> #1 _SplashState.initializeLocationAndSave (package:../screens/authentication/splash.dart:48:10) <asynchronous suspension>
``
In this page getParsedReverseGeocoding
Future<Map> getParsedReverseGeocoding(LatLng latLng) async {
var response =
json.decode(await getReverseGeocodingGivenLatLngUsingMapbox(latLng));
Map feature = response['features'][0];
Map revGeocode = {
'name': feature['text'],
'address': feature['place_name'].split('${feature['text']}, ')[1],
'place': feature['place_name'],
'location': latLng
};
return revGeocode;
}
and
void initializeLocationAndSave() async {
// Ensure all permissions are collected for Locations
Location _location = Location();
bool? _serviceEnabled;
PermissionStatus? _permissionGranted;
_serviceEnabled = await _location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await _location.requestService();
}
_permissionGranted = await _location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await _location.requestPermission();
}
// Get the current user location
LocationData _locationData = await _location.getLocation();
LatLng currentLocation =
LatLng(_locationData.latitude!, _locationData.longitude!);
// Get the current user address
String currentAddress =
(await getParsedReverseGeocoding(currentLocation))['place'];
// Store the user location in sharedPreferences
sharedPreferences.setDouble('latitude', _locationData.latitude!);
sharedPreferences.setDouble('longitude', _locationData.longitude!);
sharedPreferences.setString('current-address', currentAddress);
Navigator.pushAndRemoveUntil(context,
MaterialPageRoute(builder: (_) => const Home()), (route) => false);
}
Solution 1:[1]
From the error message, you are passing in a null value instead of a String value. Adding a null check while passing this value should help you solve this problem. For example:
Text(value ?? "Value if null is passed");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Obinna |
