我正在研究轉彎導航。
Future getReverseGeocodingGivenLatLngUsingMapbox(LatLng latLng) async {
String query = '${latLng.longitude},${latLng.latitude}';
String url = '$baseUrl/$query.json?access_token=$accessToken';
url = Uri.parse(url).toString();
print(url);
try {
_dio.options.contentType = Headers.jsonContentType;
final responseData = await _dio.get(url);
return responseData.data;
} catch (e) {
final errorMessage = DioExceptions.fromDioError(e as DioError).toString();
debugPrint(errorMessage);
}
}
上面的函式在下面使用
Future<Map> getParsedReverseGeocoding(LatLng latLng) async {
var response = 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;
}
下面使用getParsedReverseGeocoding()函式
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']; // getting error over here
// 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);
}
但是,檢索“地點”欄位時出現以下錯誤。
未處理的例外:“String”型別不是“index”的“int”型別的子型別
uj5u.com熱心網友回復:
一些應該對您有所幫助的事情:
- 看來您缺少 jsonDecode 并且:
Future getReverseGeocodingGivenLatLngUsingMapbox(LatLng latLng) async {
String query = '${latLng.longitude},${latLng.latitude}';
String url = '$baseUrl/$query.json?access_token=$accessToken';
url = Uri.parse(url).toString();
print(url);
try {
_dio.options.contentType = Headers.jsonContentType;
final responseData = await _dio.get(url);
final result = jsonDecode(responseData.data);
return result;
} catch (e) {
final errorMessage = DioExceptions.fromDioError(e as DioError).toString();
debugPrint(errorMessage);
}
}
- 建議在 Dart 中使用強型別,我猜這應該是:
Future<List<dynamic>> getReverseGeocodingGivenLatLngUsingMapbox(LatLng latLng) async {...
}
- 問題可能出在第二種方法中:
Future<Map> getParsedReverseGeocoding(LatLng latLng) async {
var response = await getReverseGeocodingGivenLatLngUsingMapbox(latLng);
// This response is likely a List and you don't have key 'features' of type String/index, i.e. you have a index which is supposed to be an int.
// Map feature = response['features'][0];
// Maybe this will work:
Map feature = response[0];
Map revGeocode = {
'name': feature['text'],
'address': feature['place_name'].split('${feature['text']}, ')[1],
'place': feature['place_name'],
'location': latLng
};
return revGeocode;
}
- 級聯呼叫會使您的代碼更難閱讀和除錯:
final revGeocode = await getParsedReverseGeocoding(currentLocation);
final currentAddress = revGeocode['place'];
// now you can hover revGeocode and see what's inside using your IDE
簡而言之:使用 Dart 型別系統,盡可能地強輸入你的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486643.html
