title: Flutter Unhandled Exception: setState() called in constructor:
body: <p>I am trying to create an application to display route and location pointer while executing getting this error.</p>
  
  <p>HomePage.dart: </p>
  
  <pre><code>class _MapState extends State&lt;Map&gt; {
  
  ///////////body of statements///////////
  
  void _addMarker(LatLng location){
    setState(() {
      _markers.add(Marker(markerId: MarkerId(location.toString()),
      position: location,
      icon: BitmapDescriptor.defaultMarker,
      ));
    });
  }
  
  void createRoute(String encodedPoly){
      setState(() {
        _polyLines.add(Polyline(polylineId: PolylineId(_lastPosition.toString()),
          width: 10,
          points: _convertToLatLng(googleMapsServices.decodePoly(encodedPoly)),
          color: black,
        ));
      });
    }
  }
  </code></pre>
  
  <p>This is inherited class placed in HomePage.dart:</p>
  
  <pre><code>class sendReq extends _MapState{
    void sendRequest(String intendedLocation)async{
      super.initState();
      List&lt;Placemark&gt; placemark = await Geolocator().placemarkFromAddress(intendedLocation);
      double latitude = placemark[0].position.latitude;
      double longitude = placemark[0].position.longitude;
      LatLng destination = LatLng(latitude,longitude);
      print("The intended location is $intendedLocation with the LatLang of $destination");
      _addMarker(destination);
      String route = await googleMapsServices.getRouteCoordinates(_lastPosition, destination);
      createRoute(route);
    }
  }
  </code></pre>
  
  <p>This is location.dart page:</p>
  
  <pre><code>onSubmitted: (value){
            sr.sendRequest(value);
          },
  
  </code></pre>
  
  <p>While running this code on location input it gives this error:</p>
  
  <pre><code>E/flutter (12680): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: setState() called in constructor: sendReq#c5316(lifecycle state: created, no widget, not mounted)
  E/flutter (12680): This happens when you call setState() on a State object for a widget that hasn't been inserted into the widget tree yet. It is not necessary to call setState() in the constructor, since the state is already assumed to be dirty when it is initially created.
  
  </code></pre>