本文介绍了location.getlocation()在android flutter上给出转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先导入并初始化位置信息包:

First importing and initializing the location package:

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

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

在运行代码的android上:

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

给出错误:

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

这仅在android上发生. Firebase和所有其他软件包似乎正在运行.我遵循了package:location/location.dart的最现代的安装规则.我正在使用2.3.5版,Flutter Doctor会显示android开发的所有复选标记.任何想法为什么会发生这种情况?

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?

推荐答案

问题可能是调用location.getLocation而不检查权限.如果您调用 location . ://github.com/Lyokone/flutterlocation/blob/master/lib/location.dart#L60"rel =" nofollow noreferrer> getLocation 方法,无需检查权限,本机在此处调用自动检查权限,如果不可用则请求.权限请求返回的结果是整数,因此是强制转换错误.

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.

您可以尝试检查权限,然后致电location.getLocation()

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

示例:

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

希望有帮助!

这篇关于location.getlocation()在android flutter上给出转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:25