我正在尝试使用flutter的geolocator sdk获取当前位置。但是我遇到了此错误。
这是堆栈跟踪
/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LastKnownLocationUsingLocationServicesTask.java:4: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
^
/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LocationUpdatesUsingLocationServicesTask.java:5: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
^
symbol: class NonNull
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':geolocator:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
我正在尝试这样获取当前位置
class _MapActivityState extends State<MapActivity> {
Position currentLocation;
@override
void initState() {
// TODO: implement initState
super.initState();
getUserLocation();
}
@override
Widget build(BuildContext context) {
return Container(
child: MapboxMap(
initialCameraPosition: CameraPosition(target: _center, zoom: 15),
onMapCreated: _onMapCreated,
myLocationEnabled: true,
),
);
}
Future<Position> locateUser() async {
return Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
getUserLocation() async {
currentLocation = await locateUser();
print(currentLocation);
}
}
感谢您的帮助
答案 0 :(得分:1)
打开LastKnownLocationUsingLocationServicesTask
文件,删除导致此错误的import语句,再次使用option + enter
导入它,我在使用geolocator
时已看到此问题。
类似地,打开LocationUpdatesUsingLocationServicesTask
文件并删除导入(有错误)并重新导入。