【问题标题】:Error: The type 'PolylineResult' used in the 'for' loop must implement 'Iterable<dynamic>'错误:“for”循环中使用的类型“PolylineResult”必须实现“Iterable<dynamic>”
【发布时间】:2021-03-05 14:20:33
【问题描述】:

我正在尝试创建一个谷歌地图并在两个坐标之间绘制一条折线。但我收到下面给出的错误 1。 我在https://dart.dev/tools/diagnostic-messages#for_in_of_invalid_type 尝试了建议的修复方法

即用result.values 替换result。在这种情况下,我得到错误 2。

谁能告诉我为什么这是一个问题(以及为什么修复不起作用)?如果您能提出解决此问题的方法,我将不胜感激?

错误 1:

Error: The type 'PolylineResult' used in the 'for' loop must implement 'Iterable<dynamic>'.
     - 'PolylineResult' is from 'package:flutter_polyline_points/src/utils/polyline_result.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_polyline_points-0.2.4/lib/src/utils/polyline_result.dart').
     - 'Iterable' is from 'dart:core'.
          for (PointLatLng point in result) {

错误 2:

Error: The getter 'values' isn't defined for the class 'PolylineResult'.
     - 'PolylineResult' is from 'package:flutter_polyline_points/src/utils/polyline_result.dart' ('../../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_polyline_points-0.2.4/lib/src/utils/polyline_result.dart').
    Try correcting the name to the name of an existing getter, or defining a getter or field named 'values'.
          for (PointLatLng point in result.values) {

代码:

void main() =>
    runApp(MaterialApp(debugShowCheckedModeBanner: false, home: MapPage()));

const double CAMERA_ZOOM = 13;
const double CAMERA_TILT = 0;
const double CAMERA_BEARING = 30;
const LatLng SOURCE_LOCATION = LatLng(42.7477863, -71.1699932);
const LatLng DEST_LOCATION = LatLng(42.6871386, -71.2143403);

const PointLatLng POINT_SOURCE_LOCATION = PointLatLng(42.7477863, -71.1699932);
const PointLatLng POINT_DEST_LOCATION = PointLatLng(42.6871386, -71.2143403);

class MapPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MapPageState();
}

class MapPageState extends State<MapPage> {
  Completer<GoogleMapController> _controller = Completer();
  Set<Marker> _markers = {};
  Set<Polyline> _polylines = {};
  List<LatLng> polylineCoordinates = [];
  PolylinePoints polylinePoints = PolylinePoints();
  String googleAPIKey = "<YOUR_API_KEY>";
  BitmapDescriptor sourceIcon;
  BitmapDescriptor destinationIcon;
  double pinPillPosition = -100;
  PinInformation currentlySelectedPin = PinInformation(
      pinPath: '',
      avatarPath: '',
      location: LatLng(0, 0),
      locationName: '',
      labelColor: Colors.grey);
  PinInformation sourcePinInfo;
  PinInformation destinationPinInfo;

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

  void setMapPins() {
    // source pin
    _markers.add(Marker(

        // This marker id can be anything that uniquely identifies each marker.
        markerId: MarkerId('sourcePin'),
        position: SOURCE_LOCATION,
        onTap: () {
          setState(() {
            currentlySelectedPin = sourcePinInfo;
            pinPillPosition = 0;
          });
        },
        icon: sourceIcon));

    sourcePinInfo = PinInformation(
        locationName: "Start Location",
        location: SOURCE_LOCATION,
        pinPath: "assets/driving_pin.png",
        avatarPath: "assets/friend1.jpg",
        labelColor: Colors.blueAccent);

    // destination pin
    _markers.add(Marker(
        // This marker id can be anything that uniquely identifies each marker.
        markerId: MarkerId('destPin'),
        position: DEST_LOCATION,
        onTap: () {
          setState(() {
            currentlySelectedPin = destinationPinInfo;
            pinPillPosition = 0;
          });
        },
        icon: destinationIcon));

    destinationPinInfo = PinInformation(
        locationName: "End Location",
        location: DEST_LOCATION,
        pinPath: "assets/destination_map_marker.png",
        avatarPath: "assets/friend2.jpg",
        labelColor: Colors.purple);
  }

  void setSourceAndDestinationIcons() async {
    sourceIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5), 'assets/driving_pin.png');

    destinationIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5),
        'assets/destination_map_marker.png');
  }

  void onMapCreated(GoogleMapController controller) {
    controller.setMapStyle(Utils.mapStyles);
    _controller.complete(controller);

    setMapPins();
    setPolylines();
  }

  @override
  Widget build(BuildContext context) {
    CameraPosition initialLocation = CameraPosition(
        zoom: CAMERA_ZOOM,
        bearing: CAMERA_BEARING,
        tilt: CAMERA_TILT,
        target: SOURCE_LOCATION);

    return Scaffold(
        body: Stack(children: <Widget>[
      GoogleMap(
        myLocationEnabled: true,
        compassEnabled: true,
        tiltGesturesEnabled: false,
        markers: _markers,
        polylines: _polylines,
        mapType: MapType.normal,
        initialCameraPosition: initialLocation,
        onMapCreated: onMapCreated,
        onTap: (LatLng location) {
          setState(() {
            pinPillPosition = -100;
          });
        },
      ),
      MapPinPillComponent(
          pinPillPosition: pinPillPosition,
          currentlySelectedPin: currentlySelectedPin)
    ]));
  }

  setPolylines() async {
    PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
      googleAPIKey,
      POINT_SOURCE_LOCATION,
      POINT_DEST_LOCATION,
    );

    if (result != null) {
      for (PointLatLng point in result) {
        polylineCoordinates.add(LatLng(point.latitude, point.longitude));
      }
    }

    setState(() {
      Polyline polyline = Polyline(
          polylineId: PolylineId("poly"),
          color: Color.fromARGB(255, 40, 122, 198),
          points: polylineCoordinates);
      _polylines.add(polyline);
    });
  }
}

class PinInformation {
  String pinPath;
  String avatarPath;
  LatLng location;
  String locationName;
  Color labelColor;

  PinInformation({this.pinPath, this.avatarPath, this.location, this.locationName, this.labelColor});
}

【问题讨论】:

    标签: flutter google-maps dart


    【解决方案1】:

    该列表存储在PolylineResultpoints 字段中。通过result.points 访问它们。

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2019-01-03
      • 2015-09-21
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2021-06-19
      • 2020-12-03
      • 1970-01-01
      相关资源
      最近更新 更多