Flutter地图当前位置:构建HomeScreen(dirty,state:_HomeScreenState#fc684)时引发了以下NoSuchMethodError

时间:2019-05-16 10:48:53

标签: flutter currentlocation

我正在使用Flutter Map和Geolocator软件包来获取当前位置并显示在地图中,但出现以下错误

getter“纬度”在null上被调用。 接收者:null 尝试致电:纬度

我已经解决了这个问题,但是没有帮助 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.

1 个答案:

答案 0 :(得分:0)

问题在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');
                  },
                ),
              ),
            ),
          ],
        ),