未处理的异常:异常:RangeError(索引):无效值:有效值范围为空:0

时间:2020-01-21 16:27:00

标签: flutter dart

这是我的代码,

import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'dart:async';
import 'package:flutter/material.dart';

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);

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

class MapPageState extends State<MapPage> {
  Completer<GoogleMapController> _controller = Completer();
  // this set will hold my markers
  Set<Marker> _markers = {};
  // this will hold the generated polylines
  Set<Polyline> _polylines = {};
  // this will hold each polyline coordinate as Lat and Lng pairs
  List<LatLng> polylineCoordinates = [];
  // this is the key object - the PolylinePoints
  // which generates every polyline between start and finish
  PolylinePoints polylinePoints = PolylinePoints();
  String googleAPIKey = "<YOUR_API_KEY>";
  // for my custom icons
  BitmapDescriptor sourceIcon;
  BitmapDescriptor destinationIcon;

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

  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');
  }

  @override
  Widget build(BuildContext context) {
    CameraPosition initialLocation = CameraPosition(
        zoom: CAMERA_ZOOM,
        bearing: CAMERA_BEARING,
        tilt: CAMERA_TILT,
        target: SOURCE_LOCATION);
    return GoogleMap(
        myLocationEnabled: true,
        compassEnabled: true,
        tiltGesturesEnabled: false,
        markers: _markers,
        polylines: _polylines,
        mapType: MapType.normal,
        initialCameraPosition: initialLocation,
        onMapCreated: onMapCreated);
  }

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

  void setMapPins() {
    setState(() {
      // source pin
      _markers.add(Marker(
          markerId: MarkerId('sourcePin'),
          position: SOURCE_LOCATION,
          icon: sourceIcon));
      // destination pin
      _markers.add(Marker(
          markerId: MarkerId('destPin'),
          position: DEST_LOCATION,
          icon: destinationIcon));
    });
  }

  setPolylines() async {

    List<PointLatLng> result = await polylinePoints?.getRouteBetweenCoordinates(
        googleAPIKey,
        SOURCE_LOCATION.latitude,
        SOURCE_LOCATION.longitude,
        DEST_LOCATION.latitude,
        DEST_LOCATION.longitude);
    if (result.isNotEmpty) {
      // loop through all PointLatLng points and convert them
      // to a list of LatLng, required by the Polyline
      result.forEach((PointLatLng point) {
        polylineCoordinates.add(LatLng(point.latitude, point.longitude));
      });
    }

    setState(() {
      // create a Polyline instance
      // with an id, an RGB color and the list of LatLng pairs
      Polyline polyline = Polyline(
          polylineId: PolylineId("poly"),
          color: Color.fromARGB(255, 40, 122, 198),
          points: polylineCoordinates);

      // add the constructed polyline as a set of points
      // to the polyline set, which will eventually
      // end up showing up on the map
      _polylines.add(polyline);
    });
   //TODO: enter code here
  }
}

当我尝试运行时出现错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: RangeError (index): Invalid value: Valid value range is empty: 0 NetworkUtil.getRouteBetweenCoordinates (package:flutter_polyline_points/src/network_util.dart:29:7)
E/flutter (28997): <asynchronous suspension>
E/flutter (28997): #1      PolylinePoints.getRouteBetweenCoordinates (package:flutter_polyline_points/flutter_polyline_points.dart:22:23)
E/flutter (28997): #2      MapPageState.setPolylines (package:routelines/main.dart:94:58)
E/flutter (28997): #3      MapPageState.onMapCreated (package:routelines/main.dart:74:7)                                 
E/flutter (28997): #4      _GoogleMapState.onPlatformViewCreated (package:google_maps_flutter/src/google_map.dart:317:14)
E/flutter (28997): #5      _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64)         
E/flutter (28997): #6      _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (28997): #7      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (28997): #8      _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
E/flutter (28997): #9      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)   
E/flutter (28997): #10     Future._propagateToListeners (dart:async/future_impl.dart:709:32)
E/flutter (28997): #11     Future._completeWithValue (dart:async/future_impl.dart:524:5)

0 个答案:

没有答案