[英]How do you handle HTTP Requests in Dart / Flutter Web Applications?
[英]How can you handle dart exceptions that have been declared in a library?
尝试处理以下代码中的异常:
findLocation() async {
final GoogleMapController controller = await _controller.future;
try {
gcd.locationFromAddress(userAddress).then((result) => controller
.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(result[0].latitude, result[0].longitude),
zoom: 15))));
} on gcd.NoResultFoundException {
print("test");
}
}
但是,当此块运行失败时,会产生以下错误:
NoResultFoundException (Could not find any result for the supplied address or coordinates.)
错误指向此代码块:
switch (platformException.code) {
case 'NOT_FOUND':
throw NoResultFoundException();
}
}
NoResultFoundException 定义在一个包含内容的类中:
/// a [Placemark] from coordinates as [double] latitude and longitude
/// or [Location] from address as [String]
class NoResultFoundException implements Exception {
/// Constructs the [LocationServiceDisabledException]
const NoResultFoundException();
@override
String toString() =>
'Could not find any result for the supplied address or coordinates.';
}
如何在findLocation()
函数中处理此错误? (顺便说一下,这是flutter地理编码库,在库文件中定义了异常。)
如果您正确await
您的电话,您只会收到例外情况。
如果您坚持在此处使用then
,那么您将需要使用.catchError
而不是传统的 try/catch 块。
忠告,不要混用await
和then
。 这是可能的,但很容易出错。 坚持一个,可能是await
,因为它更容易处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.