Can't retrieve location in Flutter by geolocator
Asked Answered
O

2

5

I'm developing a flutter app and and I made a class that retrieve current location using geolocator package and debugging by iOS Simulator and it doesn't retrieve any data. The icon of the location services just appears as hollow icon and no data is recieved.

Location class:

import 'package:geolocator/geolocator.dart';

class LocationServices {
  double latitude;
  double longitude;

  Future<void> getCurrentLocation() async {
    try {
      Position position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.best);

      latitude = position.latitude;
      longitude = position.longitude;
      print(latitude);
      print(longitude);
    } catch (e) {
      print(e);
    }
  }
}

Initstate which the function is called in:

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 4),
    );
    _opacity = Tween<double>(begin: 0.0, end: 1).animate(_animationController);
    _animationController.forward();
    NetworkManager.checkConnectivity();
    // apiServices.getPrayerTimes();
    locationServices.getCurrentLocation();
  }

Info.plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>muslim_2b</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs access to location when open.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs access to location when in the background.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>This app needs access to location when open and in the background.</string>
</dict>
</plist>

The Location icon is hollow

Overexert answered 7/6, 2021 at 10:37 Comment(1)
any error returns back by catch method ?Sinfonietta
O
6

I believe the problem can be fixed by changing the location settings on the iOS simulator. To do this:

  1. check the App menu bar of the iOS simulator. Click on 'Features' and then on 'Location'.
  2. change the Location from 'None' to a Custom Location, Apple or any other location that you wish.
  3. Run your application again.

Ordinary answered 7/6, 2021 at 11:18 Comment(0)
O
5

I also ran into this problem and I had already set my simulator's location. I resolved the issue by changing location preferences on my Mac. System Preferences -> Security & Privacy -> Enable Location Services (I enabled for both Chrome and Visual Studio Code).

Outguess answered 26/2, 2022 at 17:18 Comment(2)
its work !!! last year i didnt turn on but its work, now i got problem with nothing exception or error , you saved my life...Dissepiment
this is fixed my problem.Moreen

© 2022 - 2024 — McMap. All rights reserved.