failing to set user destination address from auto predicted places in google maps

39 Views Asked by At

flutter throwing unhandled exception error when trying to set user destination point on ridesharing flutter app

CODE

class PlacePrediction extends StatefulWidget {

  final PredictedPlaces? predictedPlaces;
  PlacePrediction({
    this.predictedPlaces
});

  @override
  State<PlacePrediction> createState() => _PlacePredictionState();
}

class _PlacePredictionState extends State<PlacePrediction> {

 getPlaceDirectionDetails(String? placeId, context) async{
   showDialog(
       context: context,
       builder: (BuildContext context)=>ProgressDialog(
         message: "Setting up Drop-Off. Please wait....",

       )
   );

   String placeDirectionDetailUrl = "https://maps.googleapis.com/maps/api/places/details/json?place_id=$placeId&key=$mapKey";
   var responseApi = await RequestAssistant.receiveRequest(placeDirectionDetailUrl);

   Navigator.pop(context);
   if(responseApi == "error occured. Failed. no response."){
     return;
   }
   if(responseApi["status"] == "OK"){
     Directions directions = Directions();
     directions.locationName = responseApi["result"]["name"];
     directions.locationId = placeId;
     directions.locationLatitude = responseApi["result"]["geometry"]["location"]["lat"];
     directions.locationLongitude = responseApi["result"]["geometry"]["location"]["lng"];

     Provider.of<AppInfo>(context, listen: false).updateDropOffLocationAddress(directions);

     setState(() {
       userDropOffAddress = directions.locationName!;
     });
     
     Navigator.pop(context,"obtainedDropoff");

   }

 }
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: (){
          getPlaceDirectionDetails(widget.predictedPlaces!.place_id, context);

        },
        style: ElevatedButton.styleFrom(
          primary: Colors.white,
        ),
        child: Padding(
          padding: EdgeInsets.all(8.0),
          child: Row(
            children: [
              const Icon(
                Icons.add_location,
                color: Colors.blue,
              ),

              SizedBox(width: 10,),
              Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        widget.predictedPlaces!.main_text!,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.green[400],
                        ),
                      ),
                      Text(
                        widget.predictedPlaces!.secondary_text!,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.green[400],
                        ),
                      ),
                    ],
                  )
              )
            ],
          ),
        ));
  }
}

Error E/flutter (15529): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' E/flutter (15529): #0 _PlacePredictionState.getPlaceDirectionDetails (package:dride/widgets/place_prediction_type.dart:42:18) E/flutter (15529): E/flutter (15529):

error coming from this part

 if(responseApi["status"] == "OK"){...}

watched several tutorial videos on YouTube but getting the same solution that is not working on my app

0

There are 0 best solutions below