flutter 试图找出设备的当前位置,它显示“MissingPluginException”

um6iljoc  于 2023-06-24  发布在  Flutter
关注(0)|答案(3)|浏览(129)

我有一段简单的代码,main.dart。任何形式的帮助都将不胜感激。我在用
地理定位器:^5.1.3
我只是想知道用户的位置。但它显示:
MissingPluginException(No implementation found for method checkPermissionStatus on channel com. baseflow. flutter/location_permissions)
必须向文本小部件提供非空字符串。'package:flutter/src/widgets/text. dart':Assert失败:第285行位置10:'data!= null'
我也在AndroidManifest中添加了这些代码:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

我的主要. dart代码是:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

  Position _currentPosition;
  String _currentAddress;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Location"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            if (_currentPosition != null) Text(_currentAddress),
            FlatButton(
              child: Text("Get location"),
              onPressed: () {
                _getCurrentLocation();
              },
            ),
          ],
        ),
      ),
    );
  }

  _getCurrentLocation() {
    geolocator
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
      });

      _getAddressFromLatLng();
    }).catchError((e) {
      print(e);
    });
  }

  _getAddressFromLatLng() async {
    try {
      List<Placemark> p = await geolocator.placemarkFromCoordinates(
          _currentPosition.latitude, _currentPosition.longitude);

      Placemark place = p[0];

      setState(() {
        _currentAddress =
        "${place.locality}, ${place.postalCode}, ${place.country}";
      });
    } catch (e) {
      print(e);
    }
  }
}
kjthegm6

kjthegm61#

您的项目是否在Android X上运行?试着这样做。
下面是另一个有用的链接:https://github.com/flutter/flutter/issues/14137

piv4azn7

piv4azn72#

关闭vscode(或android)studio,然后打开它并运行应用程序。

nafvub8i

nafvub8i3#

尝试关闭应用程序并重建

相关问题