0

我使用颤振和谷歌地图

采用地理位置来显示用户的当前位置

地图正确加载,但在加载之前,它显示此错误

'package:google_maps_flutter_platform_interface/src/types/location.dart':断言失败:第 19 行 pos 16:'纬度!= null':不正确。

这是我的 init 代码的一部分并获取当前位置

var lat;
  var long;
  
  @override
  void initState()  {
    _child = SpinKitRipple(
      itemBuilder: (BuildContext context, int index) {
        return DecoratedBox(
          decoration: BoxDecoration(
            color: index.isEven ? Colors.grey : Color(0xffffb838),
          ),
        );
      },
    );
    
    getCurrentLocation();
    populateClients();
    setCustomMapPin();
    super.initState();
  }
  

  void getCurrentLocation() async {
     LocationPermission permission;
     

     permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.deniedForever) {
    lat=6.9271;long=79.8612;
  }

  else if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission != LocationPermission.whileInUse &&
        permission != LocationPermission.always) {
      lat=6.9271;long=79.8612;
    }
  }
    else{
      
      Position res = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); //getCurrentPosition();
    
      lat=res.latitude;long=res.longitude;
      
    }
    
      
    
    
    setState(() {
      //position = res;
      //lat=6.9271;long=79.8612;
      _child = mapWidget();
    });
  }

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        title: Text('Map'),
        centerTitle: true,
        backgroundColor: Colors.blue,
      ),
      body: mapWidget(),
    );
  }

  Widget mapWidget() {
    return Stack(
      children: <Widget>[
        GoogleMap(
          initialCameraPosition: CameraPosition(
            target: LatLng(lat,long),//(position.latitude, position.longitude),
            zoom: 18,
          ),

          ///mapType: MapType.normal,
          onMapCreated: (GoogleMapController controller) async{
            _controller = controller;
            await setMapStyle(controller, context);
          },

          markers: Set<Marker>.of(markers.values),
          compassEnabled: true,
          myLocationEnabled: true,
        ),
        SizedBox(
          height: 26,
        ),
      ],
    );
  }
}

** 我想在地图正确加载之前显示动画**

4

3 回答 3

2

每当您渲染 UI 时,这取决于等待另一个操作完成,您必须使用FutureBuilder小部件。

https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

为了做你想做的事,你必须用FutureBuilder包装你当前的小部件。像这样:

      Future<Widget> reloadCurrentLocation;
    
      @override
      void initState() {
        super.initState();
    
        reloadCurrentLocation = getCurrentLocation();
      }
    
      Future<Widget> getCurrentLocation() async {
         LocationPermission permission;
         
         permission = await Geolocator.checkPermission();
         if (permission == LocationPermission.deniedForever) {
           lat=40.7128;long=74.0060;
         }
    
         else if (permission == LocationPermission.denied) {
           permission = await Geolocator.requestPermission();
         if (permission != LocationPermission.whileInUse &&
            permission != LocationPermission.always) {
            lat=40.7128;long=74.0060;
            }
        }
        else {
          Position res = await Geolocator.getCurrentPosition(desiredAccuracy: 
          LocationAccuracy.high); //getCurrentPosition();
        
          lat=res.latitude;
          long=res.longitude;          
        }
        
        populateClients();
        setCustomMapPin();
          
        return _child = mapWidget();
    }
        
    
    
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        title: Text('Map'),
        centerTitle: true,
        backgroundColor: Colors.blue,
      ),
      body: mapWidget(),
    );
  }

  Widget mapWidget() {
    return FutureBuilder(
                future: reloadCurrentLocation,
                builder: (context, state) {
                  if (state.connectionState == ConnectionState.active ||
                      state.connectionState == ConnectionState.waiting) {
                      return SpinKitRipple(
                        itemBuilder: (BuildContext context, int index) {
                          return DecoratedBox(
                               decoration: BoxDecoration(
                               color: index.isEven ? Colors.grey : 
                                    Color(0xffffb838),
                               ),
                          );
                        },
                      );
                  } else {
                      return Stack(
                        children: <Widget>[
                         GoogleMap(
                           initialCameraPosition: CameraPosition(
                           target: 
                           LatLng(lat,long),//(position.latitude, 
                           position.longitude),
                           zoom: 18,
                         ),

                         ///mapType: MapType.normal,
                         onMapCreated: (GoogleMapController 
                           controller) async{
                             _controller = controller;
                             await setMapStyle(controller, context);
                        },

                       markers: Set<Marker>.of(markers.values),
                       compassEnabled: true,
                       myLocationEnabled: true,
                       ),
                       SizedBox(
                        height: 26,
                       ),
                      ],
                     );
                });
  }
}
于 2021-02-06T08:56:12.737 回答
0

我只是添加这些行....

if (lat != null && lng != null) DisplayMap(lat: lat,lng: lng,) else CupertinoActivityIndicator()
于 2021-10-17T08:25:32.617 回答
0

在我的情况下,我已经解决了。是的,如果我们使用 FutureBuilder。但是,您需要在 getCurrentLocation() 中设置 setState((){}) 作为示例

GoogleMapController map;

Future<GoogleMapController> getCurrentLocation(GoogleMapController controller) async {
map = controller;
setState((){
//your logical function
}
return map;
}

在此之后我尝试了它,最后它在我的设备上运行了。

于 2021-08-25T16:02:56.547 回答