飞行模式开启时 getCurrentPosition 不返回位置

GoV*_*ndh 5 location flutter geolocator

插件地理定位器:^9.0.1 飞行关闭模式运行良好,

如果飞行模式打开,则无法获取位置/抛出任何错误。即使在飞行模式下我也需要获取位置

请建议更改或使用此功能的其他插件

也尝试使用位置插件,同样的情况发生

最少的代码

import 'package:flutter/material.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,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(),
    );
  }
}
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
   Position? pos;
   bool? _isLoading;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Location"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("GOT  location LAt ${pos?.latitude}"),
            Text("GOT  location long ${pos?.longitude}"),
            FlatButton(
              child: Text("Get location"),
              onPressed: () {
                getlocation();
              },
            ),
            _isLoading??false? const CircularProgressIndicator(color: Colors.green,):const SizedBox.shrink(),
          ],
        ),
      ),
    );
  }

    void getlocation() async {
    LocationPermission permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      LocationPermission _permission = await Geolocator.requestPermission();
      if (_permission == LocationPermission.denied) {
        // getlocation(forward: forward);
      } else if (permission == LocationPermission.deniedForever) {
        await Geolocator.openLocationSettings();
        // getlocation(forward: forward);
      } else {
        await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high,
        ).then((Position position) {
          // forward(position.latitude, position.longitude);
        });
      }
    } else if (permission == LocationPermission.deniedForever) {
      await Geolocator.openLocationSettings();
      // getlocation(forward: forward);
    } else {
      try {
        setState(() {
          _isLoading=true;
        });
        var er=   await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high,
        );
        print(er.longitude);
        // forward(er.latitude, er.longitude);
      } on Exception catch (er) {
        print(er.toString());
        setState(() {
          _isLoading=false;
        });
      }
      print('dd');
         await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high,
      ).then((Position position) {
        print('got loca');
        print('lat ${position.latitude}');
        print('long ${position.longitude}');
        setState(() {
          pos=position;
          _isLoading=false;
        });
        // forward(position.latitude, position.longitude);
      });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*ård 4

如果是全飞行模式(即没有任何WiFi或移动数据),手机没有连接任何东西,因此无法知道自己的位置!

因此,它也无法告诉您它的位置。