【问题标题】:Flutter map current location : The following NoSuchMethodError was thrown building HomeScreen(dirty, state:_HomeScreenState#fc684)颤振地图当前位置:在构建 HomeScreen(dirty, state:_HomeScreenState#fc684) 时引发了以下 NoSuchMethodError
【发布时间】:2019-05-16 10:48:52
【问题描述】:

我正在使用颤振地图和地理定位器包来获取当前位置并在地图中显示,但我收到如下错误

在 null 上调用了 getter 'latitude'。 接收方:空 尝试调用:纬度

我已经解决了这个问题,但没有帮助 https://github.com/johnpryan/flutter_map/issues/124

我在有状态小部件中使用过它

  LatLng _center ;
  Position currentLocation;

  Future<Position> locateUser() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  }

  getUserLocation() async {
    currentLocation = await locateUser();
    setState(() {
      _center = LatLng(currentLocation.latitude, currentLocation.longitude);
    });
    print('center $_center');
  }

  @override
  void initState() {
    super.initState();
    getUserLocation();
  }

这是我调用 getUserLocation() 的小部件

  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text('Plants Watch'),
        backgroundColor: Colors.green[700],
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.exit_to_app),
            onPressed: () {
              BlocProvider.of<AuthenticationBloc>(context).dispatch(
                LoggedOut(),
              );
            },
          )
        ],
      ),
      body: Stack(
        children: <Widget>[
          new FlutterMap(
                options: new MapOptions(
                  center: new LatLng(currentLocation.latitude, currentLocation.longitude),
                  maxZoom: 13.0,
                ),
                layers: [
                  new TileLayerOptions(
          urlTemplate: "https://api.tiles.mapbox.com/v4/"
              "{id}/{z}/{x}/{y}@2x.png?access_token={accessToken}",
          additionalOptions: {
            'accessToken': '<accessToken>',
            'id': 'mapbox.streets',
          },
        ),
        new MarkerLayerOptions(
          markers: [
            new Marker(
              width: 80.0,
              height: 80.0,
              point: LatLng(currentLocation.latitude, currentLocation.longitude),
              builder: (ctx) =>
              new Container(
                child: new IconButton(
                  icon: Icon(Icons.location_on),
                  color: Colors.green[700],
                  iconSize: 45.0,
                  onPressed: (){
                  print('Marker Tapped');
                  },
                ),
              ),
            ),
          ],
        ),
       ],
     ),
     Padding(
       padding: const EdgeInsets.all(16.0),
       child: Align(
         alignment: Alignment.bottomRight,
         child: FloatingActionButton(
           backgroundColor: Colors.green[700],
           child: Icon(Icons.add),
           onPressed: () {
             Navigator.push(context, MaterialPageRoute(builder: (context)=> PostPage()));
             },
             ),
            ),
          )
        ],
      )
    );
  }
}


I just want to remove the no such method error on building app.

【问题讨论】:

  • 首先您需要检查位置权限并尝试获取位置。在您的情况下,该位置是null,因为您无权查询它。
  • 我在 AndroidManifest 文件中定义了位置权限。你能告诉我应该在哪里定义或检查位置权限吗? @danypata
  • 对于 android 您必须检查运行时权限,对于 iOS 您需要在 info.plist 文件中添加权限。

标签: flutter currentlocation


【解决方案1】:

问题出在 MarkerLayerOption 中,我在其中调用用户当前位置,这会导致纬度错误。因此,在 FlutterMap 函数和 MarkerLayerOption 中对 LatLng 进行更改即可解决问题。

new FlutterMap(
                options: new MapOptions(
                  center: new LatLng(12.9716, 77.5946),

                  maxZoom: 13.0,
                ),
                layers: [
                  new TileLayerOptions(
          urlTemplate: "https://api.tiles.mapbox.com/v4/"
              "{id}/{z}/{x}/{y}@2x.png?access_token={accessToken}",
          additionalOptions: {
            'accessToken': '<accessToken>',
            'id': 'mapbox.streets',
          },
        ),
        new MarkerLayerOptions(
          markers: [
            new Marker(
              width: 80.0,
              height: 80.0,
              point: LatLng(12.9716, 77.5946),

              builder: (ctx) =>
              new Container(
                child: new IconButton(
                  icon: Icon(Icons.location_on),
                  color: Colors.green[700],
                  iconSize: 45.0,
                  onPressed: (){
                  print('Marker Tapped');
                  },
                ),
              ),
            ),
          ],
        ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2021-09-18
    • 2021-02-16
    相关资源
    最近更新 更多