flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

Doesn't return anything if GPS is turned off

Open praveen-gm opened this issue 6 years ago • 18 comments

Although there is Geolocator().isLocationServiceEnabled() to know if the device GPS is turned on/off and then request location. Since there is no way to turn on GPS using this plugin there needs to have a workaround to achieve the same.

Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high) doesn't return null or anything if the GPS is turned off

praveen-gm avatar Aug 05 '20 08:08 praveen-gm

I am also facing the same issues.

DhavalRKansara avatar Aug 19 '20 07:08 DhavalRKansara

geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium);
/// I set it to medium
LocationAccuracy.medium

BytesZero avatar Aug 22 '20 06:08 BytesZero

@praveen-gm Not only high every LocationAccuracy does not return anything have anyone found solution for this

AdnanKazi avatar Aug 22 '20 10:08 AdnanKazi

Solution found may be it would be useful for someone

Use this package location: ^3.0.2

Dart code

      bool enabled = await location.serviceEnabled();
      print(enabled);
      if (!enabled)
      // request location
      if (await location.requestService()) {
        // set location
        Geolocator()
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
            .then((position) async {
          print(position);
        });
      } else{
       // your else part
      }

AdnanKazi avatar Aug 22 '20 11:08 AdnanKazi

We have just released version 6.0.0-rc.1 which includes requesting to enable the location services on Android.

We would appreciate it if you could give it a try and provide feedback.

mvanbeusekom avatar Aug 22 '20 20:08 mvanbeusekom

facing same issue !

nateshmbhat avatar Aug 25 '20 06:08 nateshmbhat

@nateshmbhat which version of the geolocator plugin are you using? In our latest release candidate we have included the feature to automatically ask the user to enable the location services when calling the getCurrentPosition or getPositionStream methods.

Note that this will only work on Android with Google Play Services (so you should not use forceAndroidLocationManager). For iOS and Android using the LocationManager there are no SDKs available to automatically ask the user to enable the location services. This means you will receive a LocationServicesDisabledException which you can catch and alert the user that he or she should manually enable the location services (you can use the openLocationSettings to open the settings App, see detailed documentation here).

mvanbeusekom avatar Aug 25 '20 06:08 mvanbeusekom

Its working in 6.0.0-rc.1 but clicking on "no thanks" in the location services dialog is crashing the whole app.

D/AndroidRuntime(18533): Shutting down VM
E/AndroidRuntime(18533): FATAL EXCEPTION: main
E/AndroidRuntime(18533): Process: com.bank.falcon, PID: 18533
E/AndroidRuntime(18533): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=Intent { (has extras) }} to activity {com.rakbank.falcon/com.nuclei.falcon.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(18533): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4976)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:5017)
E/AndroidRuntime(18533): 	at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
E/AndroidRuntime(18533): 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(18533): 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(18533): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
E/AndroidRuntime(18533): 	at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(18533): 	at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.main(ActivityThread.java:7710)
E/AndroidRuntime(18533): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18533): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
E/AndroidRuntime(18533): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/AndroidRuntime(18533): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(18533): 	at com.nateshmbhat.card_scanner.CardScannerPlugin.onActivityResult(CardScannerPlugin.java:105)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEnginePluginRegistry.java:691)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry.onActivityResult(FlutterEnginePluginRegistry.java:378)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:619)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragment.onActivityResult(FlutterFragment.java:704)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragmentActivity.onActivityResult(FlutterFragmentActivity.java:508)
E/AndroidRuntime(18533): 	at android.app.Activity.dispatchActivityResult(Activity.java:8140)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4969)
E/AndroidRuntime(18533): 	... 11 more

nateshmbhat avatar Aug 25 '20 07:08 nateshmbhat

The issue has been fixed in the version https://pub.dev/packages/geolocator/versions/6.0.0-rc.3 and works as expected. as @mvanbeusekom discussed above, It now asks for the user to enable location services when getCurrentPosition() is called and when the user denies enabling location it returns a PlatformException

praveen-gm avatar Aug 25 '20 07:08 praveen-gm

@praveen-gm it is still crashing the app even in 6.0.0-rc.3 when user clicks on "no thanks"

nateshmbhat avatar Aug 25 '20 07:08 nateshmbhat

@praveen-gm thank you very much for testing and giving feedback, it is much appreciated.

@nateshmbhat I will have a look into what maybe causing this issue, thank you very much for the feedback.

mvanbeusekom avatar Aug 25 '20 07:08 mvanbeusekom

I used 6.0.0-rc.3 itself. Same crash was there when i used rc.1 too.

nateshmbhat avatar Aug 25 '20 07:08 nateshmbhat

Its working in 6.0.0-rc but clicking on "no thanks" in the location services dialog is crashing the whole app.

D/AndroidRuntime(18533): Shutting down VM
E/AndroidRuntime(18533): FATAL EXCEPTION: main
E/AndroidRuntime(18533): Process: com.bank.falcon, PID: 18533
E/AndroidRuntime(18533): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=Intent { (has extras) }} to activity {com.rakbank.falcon/com.nuclei.falcon.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(18533): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4976)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:5017)
E/AndroidRuntime(18533): 	at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
E/AndroidRuntime(18533): 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(18533): 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(18533): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2123)
E/AndroidRuntime(18533): 	at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(18533): 	at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.main(ActivityThread.java:7710)
E/AndroidRuntime(18533): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18533): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
E/AndroidRuntime(18533): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/AndroidRuntime(18533): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(18533): 	at com.nateshmbhat.card_scanner.CardScannerPlugin.onActivityResult(CardScannerPlugin.java:105)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEnginePluginRegistry.java:691)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry.onActivityResult(FlutterEnginePluginRegistry.java:378)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:619)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragment.onActivityResult(FlutterFragment.java:704)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragmentActivity.onActivityResult(FlutterFragmentActivity.java:508)
E/AndroidRuntime(18533): 	at android.app.Activity.dispatchActivityResult(Activity.java:8140)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4969)
E/AndroidRuntime(18533): 	... 11 more

you need to use try/catch around the getCurrentPosition, catch for LocationServiceDisabledException, and handle when the user taps No Thanks

praveen-gm avatar Aug 25 '20 07:08 praveen-gm

The following code crashes the application when user clicks on "No Thanks"

try {
            var location = await getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
            onLocationDetected(location);
          } catch (e) {
            getLogger().w('User denied location service !');
          }

nateshmbhat avatar Aug 25 '20 07:08 nateshmbhat

its not supposed to crash. it's supposed to raise an exception !

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 1.21.0-9.1.pre, on Mac OS X 10.15.2 19C57, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        sudo gem install cocoapods
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2020.1.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.48.0)
[✓] Connected device (3 available)

nateshmbhat avatar Aug 25 '20 07:08 nateshmbhat

@nateshmbhat the problem is not with the geolocator but in the "card_scanner" plugin, as you can see in this stack trace:

E/AndroidRuntime(18533): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(18533): 	at com.nateshmbhat.card_scanner.CardScannerPlugin.onActivityResult(CardScannerPlugin.java:105)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEnginePluginRegistry.java:691)
E/AndroidRuntime(18533): 	at io.flutter.embedding.engine.FlutterEnginePluginRegistry.onActivityResult(FlutterEnginePluginRegistry.java:378)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:619)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragment.onActivityResult(FlutterFragment.java:704)
E/AndroidRuntime(18533): 	at io.flutter.embedding.android.FlutterFragmentActivity.onActivityResult(FlutterFragmentActivity.java:508)
E/AndroidRuntime(18533): 	at android.app.Activity.dispatchActivityResult(Activity.java:8140)
E/AndroidRuntime(18533): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4969)

The card_scanner plugin also implements the ActivityResultListener interface (which is perfectly fine) and thus also receives the result when the user selects the "no thanks" option. Unfortunately the card_scanner onActivityResult method doesn't correctly test the requestCode and tries to access the FlutterResult object which at that time is null and causes the crash. You can see it in the following code snippet of the card_scanner (here is a link to the code on GitHub):

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SCAN_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if (data != null && data.hasExtra(CardScannerCameraActivity.SCAN_RESULT)) {
                CardDetails cardDetails = data.getParcelableExtra(CardScannerCameraActivity.SCAN_RESULT);
                pendingResult.success(cardDetails.toMap());
            } else {
                pendingResult.success(null);
            }

            pendingResult = null;
            return true;
        } else if (resultCode == Activity.RESULT_CANCELED) {
            pendingResult.success(null); // This line is causing the crash
            pendingResult = null;
        }
        return false;
    }

mvanbeusekom avatar Aug 25 '20 08:08 mvanbeusekom

Hey @mvanbeusekom thanks for the tip ! that was really helpful bro :D

nateshmbhat avatar Aug 25 '20 09:08 nateshmbhat

Glad I could help! I will close this issue when the final version of 6.0.0 is released (I am aiming to do so this Friday)

mvanbeusekom avatar Aug 25 '20 10:08 mvanbeusekom