Flutter:无法使用任何软件包获取位置-尝试过各种软件包-getLocation块

时间:2018-10-08 06:22:19

标签: dart flutter

所以我已经为这个问题苦苦挣扎了一段时间了,我只是想不通。我尝试使用两个不同的插件,但是它们似乎都存在问题。我尝试使用Flutter位置插件和地理位置插件。我不认为这是权限问题。很明显,每当我调用伴随着aynch的主要方法时。异步方法永远不会返回。这些是我使用的库

库1:location 1.4.1

现在,我通过以下方式将包添加到了pubspec.yaml中:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.0
  progress_hud:
     git: https://github.com/LampeMW/progress_hud/
  crypto: any
  validate: "^1.6.0"
  json_annotation: ^0.2.4
  image_picker: "^0.4.1"
  after_layout: "^1.0.4"
  firebase_messaging: #0.2.1
  device_info: ^0.2.1
#  geolocator: '^2.0.1'
  location: 
    git: https://github.com/Lyokone/flutterlocation.git/

我正以以下方式使用它

//This method blocks
  Map<String, double> location = await new Location().onLocationChanged().single;

//This method also blocks and never continues
  currentLocation  = await  myLocation.Location().getLocation();

这是我扑扑的医生的输出

[✓] Flutter (Channel beta, v0.6.0, on Mac OS X 10.13.6 17G65, locale en-US)
    • Flutter version 0.6.0 at /Users/admin/Flutter/flutter
    • Framework revision 9299c02cf7 (8 weeks ago), 2018-08-16 00:35:12 +0200
    • Engine revision e3687f70c7
    • Dart version 2.1.0-dev.0.0.flutter-be6309690f

[✓] Android toolchain - develop for Android devices (Android SDK 28.0.0)
    • Android SDK at /Users/admin/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.0
    • ANDROID_HOME = /Users/admin/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.4.1, Build version 9F2000
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.0

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] IntelliJ IDEA Ultimate Edition (version 2018.1.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 24.0.2
    • Dart plugin version 181.4445.29

[✓] Connected devices (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.1.1 (API 25) (emulator)

• No issues found!

由于第一个库出现问题,建议我使用该库,但是该库执行相同的操作(调用其方法时挂起)

更新

库2:geolocator 在我的pubspec.yaml中,添加了以下内容

dependencies:
  flutter:
  http: "^0.11.3+16"
  crypto: any
  validate: "^1.6.0"
  json_annotation: ^0.2.4
  image_picker: "^0.4.1"
  after_layout: "^1.0.4"
  firebase_messaging: #0.2.1
  device_info: ^0.2.1
  geolocator: '^2.0.1'

这是我在地理位置中使用的代码

Future<Position> getLocationWithTimeout() async
{
    Position finalPos;
    var geo = new Geolocator();
    geo.forceAndroidLocationManager = true;
    return geo.getCurrentPosition().then((Position pos){
        finalPos = pos;
        return finalPos;
    }).timeout(new Duration(milliseconds: GPS_TIMEOUT_INTERVAL_SHARED),onTimeout: (){
        log.severe("$className : Timeout happened - while retrieving position");
        return null; //Returns null
    });
}

在库2中,我将超时设置为20秒,但它只是超时。 关于如何解决此问题的任何建议?

0 个答案:

没有答案