如何解决“未处理的异常:类型'List <dynamic>'不是类型'PlaceDetails'的子类型”?

时间:2019-11-13 10:36:40

标签: flutter dart polyline currentlocation

enter image description here

我想将折线从当前位置绘制到另一个动态位置。但 有时会被绘制,但有时会在使用flutter_google_places插件搜索位置时引发异常。

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'PlaceDetails'

这里是我的一些代码:

Api:

const kGoogleApiKey = "AIz#######_###############";

GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);

在这里,我声明了一个LatLng类型的列表类型变量:

List<LatLng> latlng = [];

当前位置:

// I called this method from initState() method

void getCurrentLocation()async{
    Position pos= await Geolocator().getCurrentPosition();
    setState(() {
      position = pos;
      print("CurrentLatLng__= ${position.latitude + position.longitude }");
      latlng.add(LatLng(position.latitude,position.longitude)); // Adding current location to the list
    });
  }

Google地图:

body: GoogleMap(
            initialCameraPosition: CameraPosition(
              target: LatLng(position.latitude,position.longitude), 
              zoom: 14.4746,
            ),
            compassEnabled: true,
            myLocationEnabled: true,
           // myLocationButtonEnabled: true,
            mapType: MapType.normal,
            onMapCreated: (GoogleMapController controller){
              _controller.complete(controller);
            },
            markers: isSelectedDropff
             ?{ Marker(
                  markerId: MarkerId(globalPlaceIdDropOff),
                  infoWindow: InfoWindow(
                  title: placeNameDropOff,
                  // snippet: "${responseResult.types?.first}"
                  ),
                  icon: BitmapDescriptor.defaultMarker,
                  position: LatLng(dropOffLat , dropOffLng ),  
               ),

              }:
              { 
                Marker(
                  markerId: MarkerId("current loc"),
                  infoWindow: InfoWindow(
                   title: "Current",
                   // snippet: "${responseResult.types?.first}"
                  ),
                  icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
                  position: LatLng(position.latitude, position.longitude),
                ),


              },
              polylines: isSelectedDropOff ?{
                Polyline(
                  polylineId: PolylineId("One") , 
                  points:latlng,
                  color: Colors.red,
                  visible: true
                )
              }
              :null,
          ),

自动完成器:

onTap: ()async{
                    // show input autocomplete with selected mode
                    // then get the Prediction selected
                    Prediction p =await PlacesAutocomplete.show(
                      context: context,
                      apiKey: kGoogleApiKey,
                      mode: Mode.overlay, // Mode.fullscreen
                      language: "fr",
                      components: [new Component(Component.country, "fr")]
                    );
                    displayPredictionDropOff(p);                
                  },



// called it from TextField widget with it's constructor onTap:

Future<Null> displayPredictionDropOff(Prediction p) async {
    if (p != null) {
      PlacesDetailsResponse detail =
      await _places.getDetailsByPlaceId(p.placeId); //Note: exception occurring at this line

      var placeId = p.placeId;
      double lat = detail.result.geometry.location.lat;
      double lng = detail.result.geometry.location.lng;

      var address = await Geocoder.local.findAddressesFromQuery(p.description);
      setState(() {
        placeNameDropOff = detail.result.name;
        dropOffLat = lat;
        dropOffLng = lng;
        globalPlaceIdDropOff = placeId;
        isSelectedDropOff = true;
        latlng.add(LatLng(lat,lng)); // add second location to the list
      });


      print("lat------$lat");
      print("lng--------$lng");
      print("description-----${p.description}");
      print("name------${detail.result.name}");
      print("address----------$address");

      final GoogleMapController controller = await _controller.future;
      controller.animateCamera(CameraUpdate.newCameraPosition(
        CameraPosition(
          target: LatLng(lat, lng),
          zoom: 19.151926040649414
        ),
      ));
    }
  }

注意:发生在下面一行的异常(未处理的异常:“列表”类型不是“ PlaceDetails”类型的子类型)

PlacesDetailsResponse detail =
      await _places.getDetailsByPlaceId(p.placeId);

0 个答案:

没有答案