Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何使用Flatter中的geoLocator软件包在控制台上打印当前位置?_Flutter_Dart_Location_Emulation - Fatal编程技术网

Flutter 如何使用Flatter中的geoLocator软件包在控制台上打印当前位置?

Flutter 如何使用Flatter中的geoLocator软件包在控制台上打印当前位置?,flutter,dart,location,emulation,Flutter,Dart,Location,Emulation,这是我在加载的screen.dart文件中的代码。我清楚地提到了打印我的位置,甚至在onpressed中调用了该方法。还是没有反应 import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; class LoadingScreen extends StatefulWidget { @override _LoadingScreenState createState() =>

这是我在加载的screen.dart文件中的代码。我清楚地提到了打印我的位置,甚至在onpressed中调用了该方法。还是没有反应

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

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {

    void getLocation() async{
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    print(position);
    }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () {
            getLocation();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:地理定位器/地理定位器.dart”;
类加载屏幕扩展StatefulWidget{
@凌驾
_LoadingScreenState createState()=>\u LoadingScreenState();
}
类加载Screenstate扩展状态{
void getLocation()异步{
位置=等待地理定位器。getCurrentPosition(期望精度:定位精度。低);
印刷(职位);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:升起按钮(
已按下:(){
getLocation();
},
子项:文本(“获取位置”),
),
),
);
}
}

您的实现缺少几个步骤

首先,您需要检查权限,如果允许,则获取位置,否则请求权限

您还需要在android的清单和ios的info.plist中添加权限

示例代码:-

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Test(),
    );
  }
}

class Test extends StatefulWidget{
  @override
  _Test createState() => _Test();
}

class _Test extends State<Test>{

  void getLocation() async{
    LocationPermission permission = await Geolocator.checkPermission();
    if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
      printLocation();
    }else{
      requestPermission();
    }
  }

  requestPermission() async{
    LocationPermission permission = await Geolocator.requestPermission();
    if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
      printLocation();
    }else{
      requestPermission();
    }
  }

  printLocation() async{
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low, timeLimit: Duration(seconds: 10));
    print(position);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () {
            getLocation();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:地理定位器/地理定位器.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:Test(),
);
}
}
类测试扩展了StatefulWidget{
@凌驾
_Test createState()=>u Test();
}
类测试扩展了状态{
void getLocation()异步{
LocationPermission=等待地理定位器。checkPermission();
if(permission==LocationPermission.always | | permission==LocationPermission.whileInUse){
printLocation();
}否则{
requestPermission();
}
}
requestPermission()异步{
LocationPermission=Wait Geolocator.requestPermission();
if(permission==LocationPermission.always | | permission==LocationPermission.whileInUse){
printLocation();
}否则{
requestPermission();
}
}
printLocation()异步{
位置位置=等待地理定位器。getCurrentPosition(期望精度:LocationAccurance.low,时限:持续时间(秒:10));
印刷(职位);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:升起按钮(
已按下:(){
getLocation();
},
子项:文本(“获取位置”),
),
),
);
}
}

你在iOS模拟器上运行过吗?没有,在android模拟器上运行过。实际上,模拟器本身造成了一个小问题。但是我通过删除并创建一个新的来修复它:P