Error: Type 'Geolocator' not found and 'Placemark' isn't a type
Error: Type 'Geolocator' not found and 'Placemark' isn't a type
我收到此错误:Type 'Geolocator' not found
和 'Placemark' isn't a type
。每当我尝试执行 flutter run
时,我都会收到此错误:
lib/screens/register.dart:33:9: Error: Type 'Geolocator' not found.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:33:9: Error: Type 'Geolocator' not found.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:33:9: Error: 'Geolocator' isn't a type.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:33:33: Error: Method not found: 'Geolocator'.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:108:12: Error: 'Placemark' isn't a type.
List<Placemark> p = await geolocator.placemarkFromCoordinates(
^^^^^^^^^
lib/screens/register.dart:111:7: Error: 'Placemark' isn't a type.
Placemark place = p[0];
^^^^^^^^^
lib/screens/profile_edit.dart:33:9: Error: 'Geolocator' isn't a type.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:33:34: Error: Method not found: 'Geolocator'.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:70:12: Error: 'Placemark' isn't a type.
List<Placemark> p = await _geolocator.placemarkFromCoordinates(
^^^^^^^^^
lib/screens/profile_edit.dart:73:7: Error: 'Placemark' isn't a type.
Placemark place = p[0];
^^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 896
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 24s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 25.3s
Exception: Gradle task assembleDebug failed with exit code 1
为什么我运行陷入这个问题,那个,我还在搞清楚。
它是如何开始的?
在我做 flutter upgrade
之前。在处理我的电力时,这个过程被打断了,因此,flutter 被破坏了。
然后我做了什么?
我重新安装了Flutter,然后做了一些改动。做了这些:
- Flutter pubspec.lock 文件删除
- 运行扑干净
- 运行扑运行
它以某种方式解决了我的包裹问题,但我在 运行ning 时仍然遇到错误。然后我就这么做了
flutter upgrade --force // which gave me that I am on stable and upgraded
flutter pub cache repair // which downloaded the packages again, except few failures which I ignored, not related to geolocator
flutter clean
flutter run
又是我上面提到的同样的错误。
现在,我规定的是,存在某种包弃用问题,但我已经在使用最新的包 geolocator 6.0.0+1
告诉我 flutter doctor -v
或 flutter doctor
给你
[√] Flutter (Channel stable, 1.20.3, on Microsoft Windows [Version 10.0.18363.1016],
locale en-US)
• Flutter version 1.20.3 at C:\flutter
• Framework revision 216dee60c0 (2 days ago), 2020-09-01 12:24:47 -0700
• Engine revision d1bc06f032
• Dart version 2.9.2
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• ANDROID_SDK_ROOT = C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 44.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] VS Code (version 1.48.2)
• VS Code at C:\Users\ALOK KUMAR\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.13.2
[√] Connected device (1 available)
• ONEPLUS A6010 (mobile) • 4ee3072b • android-arm64 • Android 10 (API 29)
• No issues found!
我该如何解决这个问题?
这确实是软件包 geolocator
中已弃用的 Geolocator()
的问题。现在有了新的flutter版本1.20.3
和geolocator 6.0.0+1
,需要使用包geocoder 0.2.1获取地址
以下代码已完全弃用,完全不受支持:
// for getting the position
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
// for getting location address
List<Placemark> p = await geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress =
"${place.locality}, ${place.postalCode}, ${place.country}";
});
要获取地址,您现在可以同时遵循 geolocator 和 geocoder
文档。反正我是通过我的代码指出来的:
算法:
- 您从 Geolocator 获取位置
- 您只能从
Geolocator Position
开始 forceAndroidLocationManager: true
- 您从
Geocoder
包裹中获取地址
代码:
// you can play with the accuracy as per your need. best, high, low etc
// by default forceAndroidLocationManager is false
Position position = await getCurrentPosition(desiredAccuracy: LocationAccuracy.high,
forceAndroidLocationManager: true).catchError((err) => print(err));
// this will get the coordinates from the lat-long using Geocoder Coordinates
final coordinates = Coordinates(position.latitude, position.longitude);
// this fetches multiple address, but you need to get the first address by doing the following two codes
var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
var first = addresses.first;
print(place.countryName); // your country name will come. You can play with it to find out more
这就是您如何修复已弃用的 Geolocator
代码。
我认为您的问题是由于 from Geolocator version 6.0.0rc.1 他们将地理编码分离到不同的插件。
如果将 Geolocator 与地理编码一起导入,则可以使用“placemarkFromCoordinates”和 Placemark,这样您的代码才能正常工作。
我添加了一个代码示例:
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
...
// Future builder with GoogleMap widget or whatever...
...
// Method to get current position:
Future<void> _getCurrentPosition() async {
// verify permissions
LocationPermission permission = await checkPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
permission = await requestPermission();
}
// get current position
_currentPosition =
await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
// get address
String _currentAddress = await _getGeolocationAddress(_currentPosition);
}
// Method to get Address from position:
Future<String> _getGeolocationAddress(Position position) async {
// geocoding
var places = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
if (places != null && places.isNotEmpty) {
final Placemark place = places.first;
return "${place.thoroughfare}, ${place.locality}";
}
return "No address availabe";
}
只是对之前的回答做一些更正
pubspec.yaml 和
geolocator: ^6.1.5
geocoding: 1.0.5
这是代码
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
Future<void> _getCurrentPosition() async {
// verify permissions
LocationPermission permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
await Geolocator.openAppSettings();
await Geolocator.openLocationSettings();
}
// get current position
_currentPosition = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
// get address
String _currentAddress = await _getGeolocationAddress(_currentPosition);
}
// Method to get Address from position:
Future<String> _getGeolocationAddress(Position position) async {
// geocoding
var places = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
if (places != null && places.isNotEmpty) {
final Placemark place = places.first;
return "${place.thoroughfare}, ${place.locality}";
}
return "No address available";
}
我收到此错误:Type 'Geolocator' not found
和 'Placemark' isn't a type
。每当我尝试执行 flutter run
时,我都会收到此错误:
lib/screens/register.dart:33:9: Error: Type 'Geolocator' not found.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:33:9: Error: Type 'Geolocator' not found.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:33:9: Error: 'Geolocator' isn't a type.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:33:33: Error: Method not found: 'Geolocator'.
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/register.dart:108:12: Error: 'Placemark' isn't a type.
List<Placemark> p = await geolocator.placemarkFromCoordinates(
^^^^^^^^^
lib/screens/register.dart:111:7: Error: 'Placemark' isn't a type.
Placemark place = p[0];
^^^^^^^^^
lib/screens/profile_edit.dart:33:9: Error: 'Geolocator' isn't a type.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:33:34: Error: Method not found: 'Geolocator'.
final Geolocator _geolocator = Geolocator()..forceAndroidLocationManager;
^^^^^^^^^^
lib/screens/profile_edit.dart:70:12: Error: 'Placemark' isn't a type.
List<Placemark> p = await _geolocator.placemarkFromCoordinates(
^^^^^^^^^
lib/screens/profile_edit.dart:73:7: Error: 'Placemark' isn't a type.
Placemark place = p[0];
^^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 896
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 24s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 25.3s
Exception: Gradle task assembleDebug failed with exit code 1
为什么我运行陷入这个问题,那个,我还在搞清楚。
它是如何开始的?
在我做 flutter upgrade
之前。在处理我的电力时,这个过程被打断了,因此,flutter 被破坏了。
然后我做了什么?
我重新安装了Flutter,然后做了一些改动。做了这些:
- Flutter pubspec.lock 文件删除
- 运行扑干净
- 运行扑运行
它以某种方式解决了我的包裹问题,但我在 运行ning 时仍然遇到错误。然后我就这么做了
flutter upgrade --force // which gave me that I am on stable and upgraded
flutter pub cache repair // which downloaded the packages again, except few failures which I ignored, not related to geolocator
flutter clean
flutter run
又是我上面提到的同样的错误。
现在,我规定的是,存在某种包弃用问题,但我已经在使用最新的包 geolocator 6.0.0+1
告诉我 flutter doctor -v
或 flutter doctor
给你
[√] Flutter (Channel stable, 1.20.3, on Microsoft Windows [Version 10.0.18363.1016],
locale en-US)
• Flutter version 1.20.3 at C:\flutter
• Framework revision 216dee60c0 (2 days ago), 2020-09-01 12:24:47 -0700
• Engine revision d1bc06f032
• Dart version 2.9.2
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• ANDROID_SDK_ROOT = C:\Users\ALOKKU~1\AppData\Local\Android\Sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 44.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] VS Code (version 1.48.2)
• VS Code at C:\Users\ALOK KUMAR\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.13.2
[√] Connected device (1 available)
• ONEPLUS A6010 (mobile) • 4ee3072b • android-arm64 • Android 10 (API 29)
• No issues found!
我该如何解决这个问题?
这确实是软件包 geolocator
中已弃用的 Geolocator()
的问题。现在有了新的flutter版本1.20.3
和geolocator 6.0.0+1
,需要使用包geocoder 0.2.1获取地址
以下代码已完全弃用,完全不受支持:
// for getting the position
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
// for getting location address
List<Placemark> p = await geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress =
"${place.locality}, ${place.postalCode}, ${place.country}";
});
要获取地址,您现在可以同时遵循 geolocator 和 geocoder
文档。反正我是通过我的代码指出来的:
算法:
- 您从 Geolocator 获取位置
- 您只能从
Geolocator Position
开始forceAndroidLocationManager: true
- 您从
Geocoder
包裹中获取地址
代码:
// you can play with the accuracy as per your need. best, high, low etc
// by default forceAndroidLocationManager is false
Position position = await getCurrentPosition(desiredAccuracy: LocationAccuracy.high,
forceAndroidLocationManager: true).catchError((err) => print(err));
// this will get the coordinates from the lat-long using Geocoder Coordinates
final coordinates = Coordinates(position.latitude, position.longitude);
// this fetches multiple address, but you need to get the first address by doing the following two codes
var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
var first = addresses.first;
print(place.countryName); // your country name will come. You can play with it to find out more
这就是您如何修复已弃用的 Geolocator
代码。
我认为您的问题是由于 from Geolocator version 6.0.0rc.1 他们将地理编码分离到不同的插件。 如果将 Geolocator 与地理编码一起导入,则可以使用“placemarkFromCoordinates”和 Placemark,这样您的代码才能正常工作。 我添加了一个代码示例:
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
...
// Future builder with GoogleMap widget or whatever...
...
// Method to get current position:
Future<void> _getCurrentPosition() async {
// verify permissions
LocationPermission permission = await checkPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
permission = await requestPermission();
}
// get current position
_currentPosition =
await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
// get address
String _currentAddress = await _getGeolocationAddress(_currentPosition);
}
// Method to get Address from position:
Future<String> _getGeolocationAddress(Position position) async {
// geocoding
var places = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
if (places != null && places.isNotEmpty) {
final Placemark place = places.first;
return "${place.thoroughfare}, ${place.locality}";
}
return "No address availabe";
}
只是对之前的回答做一些更正
pubspec.yaml 和
geolocator: ^6.1.5
geocoding: 1.0.5
这是代码
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
Future<void> _getCurrentPosition() async {
// verify permissions
LocationPermission permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
await Geolocator.openAppSettings();
await Geolocator.openLocationSettings();
}
// get current position
_currentPosition = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
// get address
String _currentAddress = await _getGeolocationAddress(_currentPosition);
}
// Method to get Address from position:
Future<String> _getGeolocationAddress(Position position) async {
// geocoding
var places = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
if (places != null && places.isNotEmpty) {
final Placemark place = places.first;
return "${place.thoroughfare}, ${place.locality}";
}
return "No address available";
}