首页 > 解决方案 > 颤抖为什么我不能在屏幕上打印设备的位置

问题描述

我试图打印我的模拟设备的位置,但它只是打印一堆东西而不是设备的位置。我已经检查并且设备的位置已打开。

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(LocationApp());
}

class LocationApp extends StatelessWidget {
  void locationGet() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.low);
    print(position);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: RaisedButton(
            onPressed: () {
              locationGet();
            },
            child: Text('LOCATION'),
          ),
        ),
      ),
    );
  }
}

这就是我在设备位置打开时遇到的错误,我什至在提示我时允许使用位置

E/flutter ( 7183): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: The location service on the device is disabled.
E/flutter ( 7183): #0      MethodChannelGeolocator._handlePlatformException (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:173:9)
E/flutter ( 7183): #1      MethodChannelGeolocator.getPositionStream.<anonymous closure> (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:152:11)
E/flutter ( 7183): #2      _invokeErrorHandler (dart:async/async_error.dart:16:24)
E/flutter ( 7183): #3      _HandleErrorStream._handleError (dart:async/stream_pipe.dart:282:9)
E/flutter ( 7183): #4      _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter ( 7183): #5      _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter ( 7183): #6      _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter ( 7183): #7      _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter ( 7183): #8      _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter ( 7183): #9      _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter ( 7183): #10     _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:294:7)
E/flutter ( 7183): #11     _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:132:11)
E/flutter ( 7183): #12     _ForwardingStream._handleError (dart:async/stream_pipe.dart:97:10)
E/flutter ( 7183): #13     _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:161:13)
E/flutter ( 7183): #14     _rootRunBinary (dart:async/zone.dart:1214:47)
E/flutter ( 7183): #15     _CustomZone.runBinary (dart:async/zone.dart:1107:19)
E/flutter ( 7183): #16     _CustomZone.runBinaryGuarded (dart:async/zone.dart:1013:7)
E/flutter ( 7183): #17     _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:376:15)
E/flutter ( 7183): #18     _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:394:16)
E/flutter ( 7183): #19     _DelayedError.perform (dart:async/stream_impl.dart:622:14)
E/flutter ( 7183): #20     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter ( 7183): #21     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
E/flutter ( 7183): #22     _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 7183): #23     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 7183): #24     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 7183): #25     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 7183): #26     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 7183): #27     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 7183): #28     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 7183): #29     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 7183): #30     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7183): #31     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 7183): 
D/EGL_emulation( 7183): eglMakeCurrent: 0xd921a120: ver 2 0 (tinfo 0xd920f1e0)

标签: flutterdartasync-await

解决方案


您可以尝试请求许可:

LocationPermission permission = await Geolocator.checkPermission();
LocationPermission permission = await Geolocator.requestPermission();

从这里得到它: https ://pub.dev/packages/geolocator

此外,为了打印位置,您应该打印如下内容:

print(position == null ? 'Unknown' : position.latitude.toString() + ', ' + position.longitude.toString());
});

推荐阅读