Search code examples
flutterlocationgeolocator

getCurrentPosition not returning location with airplane Mode ON


Plugin geolocator: ^9.0.1 with flight off mode works well,

Not getting location /throwing any error if flight mode is on. I need to get location even in airplane mode

Kindly suggest changes or other plugin with this feature

Tried also with location plugin same happens

Minimal code

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);
      });
    }
  }
}

Solution

  • If it's the total airplane mode (i.e. without any WiFi or mobile data), the phone is not connected to anything, thus has no way of knowing its own position!

    And therefore, it can't tell you its position, either.