joshLor
joshLor

Reputation: 1074

location.getlocation() gives casting error on android flutter

First importing and initializing the location package:

import 'package:location/location.dart' as locationPackage;

locationPackage.Location location = new locationPackage.Location(); 

On android running the code:

try {
  await location.getLocation().then((pos) {
    setState(() {
      print(pos.runtimeType);
      _center = LatLng(pos.latitude, pos.longitude);;
      _lastMapPosition = _center;
    });
  });
} catch (e) {
  print(e);
  if (e.code == 'PERMISSION_DENIED') {
   print("Permission Denied");
  }
}
}

Gives the error:

I/flutter ( 4326): NoSuchMethodError: Class 'int' has no instance method 'cast'.
I/flutter ( 4326): Receiver: 1
I/flutter ( 4326): Tried calling: cast<String, double>()
E/flutter ( 4326): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: Class 'NoSuchMethodError' has no instance getter 'code'.

E/flutter ( 4326): Receiver: Instance of 'NoSuchMethodError'
E/flutter ( 4326): Tried calling: code
E/flutter ( 4326): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter ( 4326): #1      _MapsSearchPageState._getLocation (package:Bartr/maps.dart:486:13)
E/flutter ( 4326): <asynchronous suspension>
E/flutter ( 4326): #2      _MapsSearchPageState.initState (package:Bartr/maps.dart:500:5)
E/flutter ( 4326): #3      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3846:58)
E/flutter ( 4326): #4      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3717:5)
E/flutter ( 4326): #5      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #6      Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #7      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter ( 4326): #8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #9      Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #10     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16)
E/flutter ( 4326): #11     Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5)
E/flutter ( 4326): #12     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3722:5)
E/flutter ( 4326): #13     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3717:5)
E/flutter ( 4326): #14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #16     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter ( 4326): #17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #19     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter ( 4326): #20     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #22     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter ( 4326): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter ( 4326): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16)
E/flutter ( 4326): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5)
E/flutter ( 4326): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3722:5)
E/flutter ( 4326): #31     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
E/flutter ( 4326): #32     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3717:5)
E/flutter ( 4326): #33     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #35     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16)
E/flutter ( 4326): #36     Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5)
E/flutter ( 4326): #37     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3722:5)
E/flutter ( 4326): #38     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3717:5)
E/flutter ( 4326): #39     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2961:14)
E/flutter ( 4326): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2764:12)
E/flutter ( 4326): #41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3750:16)
E/flutter ( 4326): #42     Element.rebuild (package:flutter/src/widgets/framework.dart:3565:5)
E/flutter ( 4326): #43     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3722:5)
E/flutter ( 4326): #44     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
E/flutter ( 4326): #45     Componen

This only happens on android. Firebase and all other packages appear to be working. I have followed the most modern installation rules for package:location/location.dart. I am using version 2.3.5 and flutter doctor shows all checkmarks for android development. Any ideas why this is happening?

Upvotes: 4

Views: 5221

Answers (2)

Russo
Russo

Reputation: 2992

Solved by using geolocator 5.3.1 by baseflow.com https://pub.dev/packages/geolocator#-readme-tab-

and read the tutorial here: https://alligator.io/flutter/geolocator-plugin/

Tested working on my Android Studio emulator

Upvotes: 0

Hemanth Raj
Hemanth Raj

Reputation: 34769

The issue might be calling location.getLocation without checking for permissions. Looking at the plugin location, if you invoke the getLocation method without checking for permissions, the native call here invoked automatically checks for permission, requests if not available. The result returned for permission request is an integer and hence the cast error.

An issue should be raised regarding this at the plugin location to either return proper results or throw appropriate errors.

You can try check for permission and then call location.getLocation()

Example:

void _getLocation() async {
    Location location = Location();
    if (await location.hasPermission()) {
        final LocationData pos = await location.getLocation();
        setState(() {
            print(pos.runtimeType);
            _lastMapPosition = pos;
        });
    } else {
        await location.requestPermission();
    }
}

Hope that helps!

Upvotes: 3

Related Questions