【发布时间】:2020-11-23 02:48:25
【问题描述】:
顺便说一句 - 事情在 IOS 上按预期工作。 我遇到的问题是在 Android (Nexus 5 API 30) 上运行
我已仔细按照instructions 进行设置,特别注意 gradle.properties 和 "android/app/build.gradle" AndroidManifest.xml 也更新为包含
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION
uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION
这里是调用地理定位的代码...
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void initState() {
super.initState();
getLocation();
}
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
当我运行代码时,有很长的异常列表,前几个是:
E/flutter (21698): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: The location service on the device is disabled.
E/flutter (21698): #0 MethodChannelGeolocator._handlePlatformException (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:192:9)
E/flutter (21698): #1 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:121:7)
E/flutter (21698): <asynchronous suspension>
E/flutter (21698): #2 Geolocator.getCurrentPosition (package:geolocator/geolocator.dart:258:35)
【问题讨论】:
-
检查设备是否开启定位功能
标签: android flutter geolocation