title: Flutter: How to clear polylines from Google Maps
body: <p>I have created a Google Maps widget with markers and polylines with the code below.</p>
  
  <p>Everything works fine, except that when I draw a polyline between two coordinates, and later try to remove it, the polyline doesn't get removed (at least not permanently). When I use the methods below to try to remove the old polyline, and add a new one I get two polylines:</p>
  
  <pre><code>_polyLine.clear()
  _polyLine.remove("id")
  polylines.removeWhere((m) =&gt; m.polylineId.value == 'polyId$_randPolyId')
  </code></pre>
  
  <p>It gets removed and then added again in addition to the new one. This results in the picture below:</p>
  
  <p><a href="https://i.stack.imgur.com/9nEo3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9nEo3.png" alt="The Results"></a>
  ...</p>
  
  <pre><code>void showPinsOnMap() {        
        // _polylines.removeWhere(
        // (m) =&gt; m.polylineId.value == 'polyId$_randPolyId');
        // _polylines.clear()
        _polylines = {};
        print("\n\n\nPolyLines Lenght: ${_polylines.length}\n\n\n");
  
     var pinPosition = LatLng(_currentLocation.latitude,_currentLocation.longitude);
  
     var destPosition = LatLng(_destinationLocation.latitude,
     _destinationLocation.longitude);
     _markers.add(Marker(
        markerId: MarkerId('sourcePin'),
        position: pinPosition,
        icon: _sourceIcon
     ));
     _markers.add(Marker(
        markerId: MarkerId('destPin'),
        position: destPosition,
        icon: _destinationIcon,
     ));
  
     setPolylines();
     notifyListeners();
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>...
  void setPolylines() async {
     List&lt;PointLatLng&gt; result = await _polylinePoints.getRouteBetweenCoordinates(
     _googleAPIKey,
     _currentLocation.latitude,
     _currentLocation.longitude,
     _destinationLocation.latitude,
     _destinationLocation.longitude).catchError((onError){
       print("\n\n\n An Error Occured Possibly dure to network issues\n\n\n");
     });
  
    if(result != null){
      print("Results not null");
     if(result.isNotEmpty){
        result.forEach((PointLatLng point){
           _polylineCoordinates.add(
              LatLng(point.latitude,point.longitude)
           );
        });
         _isLoadingData = false;
        _polylines.add(Polyline(
          width: 5, 
          polylineId: PolylineId('polyId$_randPolyId'),
          color:
          Color.fromARGB(255, 40, 122, 198), 
          points: _polylineCoordinates
          ));
    }
    }
    notifyListeners();
  }
  ...
  </code></pre>