getter 'placeName' 被调用为 null
The getter 'placeName' was called on null
我用以下字符串创建了一个 class 地址
class Address{
String placeName='';
double latitude;
double longitude;
String placeId= '';
String placeFormattedAddress= '';
Address(
{
this.placeId,
this.latitude,
this.longitude,
this.placeName,
this.placeFormattedAddress,
});
}
我尝试从 google 地图获取地址,然后使用 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;
}
}
但问题是这需要一些时间
所以,当我尝试打开将显示地址的页面时,它显示错误
地址准备好后它就消失了,那么有什么办法可以防止这种情况发生吗?
在地址准备好之前传递不同的值 ???
getter 'placeName' 被调用为 null。
接收者:空
尝试调用:placeName
String address = Provider.of<AppData>(context).pickupAddress.placeName ?? '';
and then I print or display the content of the address
您可能想在 UI 代码中使用条件。像 :
if(placeName != null)
Text(placeName)
// null-aware operator
Text(placeName ?? "name is null")
// ternary operator
placeName != null ? Text(placeName) : CircularProgressIndicator()
更新:
太简单了,但我以前看不到
只需在 .pickupaddress
之后添加问号 (?)
String address = Provider.of<AppData>(context).pickupAddress?.placeName ?? '';
我用以下字符串创建了一个 class 地址
class Address{
String placeName='';
double latitude;
double longitude;
String placeId= '';
String placeFormattedAddress= '';
Address(
{
this.placeId,
this.latitude,
this.longitude,
this.placeName,
this.placeFormattedAddress,
});
}
我尝试从 google 地图获取地址,然后使用 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;
}
}
但问题是这需要一些时间 所以,当我尝试打开将显示地址的页面时,它显示错误 地址准备好后它就消失了,那么有什么办法可以防止这种情况发生吗? 在地址准备好之前传递不同的值 ???
getter 'placeName' 被调用为 null。 接收者:空 尝试调用:placeName
String address = Provider.of<AppData>(context).pickupAddress.placeName ?? '';
and then I print or display the content of the address
您可能想在 UI 代码中使用条件。像 :
if(placeName != null)
Text(placeName)
// null-aware operator
Text(placeName ?? "name is null")
// ternary operator
placeName != null ? Text(placeName) : CircularProgressIndicator()
更新: 太简单了,但我以前看不到 只需在 .pickupaddress
之后添加问号 (?)String address = Provider.of<AppData>(context).pickupAddress?.placeName ?? '';