【发布时间】:2022-01-05 04:22:37
【问题描述】:
我正在尝试使用GeoLocator 获取我当前位置的纬度和经度。 我阅读了文档。 我是 Flutter 初学者 我在 info.split 中添加了键、字符串,但错误并没有消失。 我搜索过,但我遇到了麻烦,因为我找不到解决方案。
错误信息
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: User denied permissions to access the device's location.
#0 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:127:7)
<asynchronous suspension>
#1 _MyHomePageState.getLocation (package:applicationname/main.dart:37:25)
<asynchronous suspension>
我添加了 main.dart 文件。 我将在这段代码中获取位置数据。
main.dart
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _location = "no data";
getLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
setState(() {
_location = position.toString();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_location),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'_location',
),
Text(
'_location',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getLocation, child: Icon(Icons.location_on)),
);
}
}
【问题讨论】:
-
你在android清单文件中添加了位置权限吗?
-
是的。我在问题文本中添加了 AndroidManifest.xml 的代码。
-
没关系。错误在于无法获取位置。如果可能,添加 main.dart 文件。
-
如果您想获取当前位置,请参考我的回答 here。
标签: flutter dart geolocation