I created a class Address with the following strings
class Address{
String placeName='';
double latitude;
double longitude;
String placeId= '';
String placeFormattedAddress= '';
Address(
{
this.placeId,
this.latitude,
this.longitude,
this.placeName,
this.placeFormattedAddress,
});
}
and I tried to get the address from google maps and then pass the value using Provider Package
class HelperMethods{
static Future<String> findCordinateAddress(Position position, context) async {
String placeAddress = '';
var connectivityResult = await Connectivity().checkConnectivity();
if(connectivityResult != ConnectivityResult.mobile && connectivityResult != ConnectivityResult.wifi){
return placeAddress;
}
String url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.latitude},${position.longitude}&key=$mapKey';
var response = await RequestHelper.getRequest(url);
if(response != 'failed'){
placeAddress = response['results'][0]['formatted_address'];
Address pickupAddress = new Address();
pickupAddress.longitude = position.longitude;
pickupAddress.latitude = position.latitude;
pickupAddress.placeName = placeAddress;
Provider.of<AppData>(context, listen: false).updatePickupAddress(pickupAddress);
}
return placeAddress;
}
}
but the thing is that it takes some time so, when I try to open the page where the address will be displayed it shows the error and it disappeared after the address is ready, so is there anyway to prevent this from happening ? pass a different value until the address is ready ???
The getter 'placeName' was called on null. Receiver: null Tried calling: placeName
String address = Provider.of<AppData>(context).pickupAddress.placeName ?? '';
and then I print or display the content of the address
UPDATE : it was so easy but I couldn't see it before just add question mark (?) after .pickupaddress
String address = Provider.of<AppData>(context).pickupAddress?.placeName ?? '';