gpt4 book ai didi

flutter - 如何处理已在库中声明的 dart 异常?

转载 作者:行者123 更新时间:2023-12-03 04:12:41 25 4
gpt4 key购买 nike

尝试在下面的代码中处理异常:


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");
}
}
但是,当此 block 运行不成功时,会产生以下错误:
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,您只会得到异常(exception)。你的电话正确。
如果您坚持使用 then在这里,你需要使用 .catchError而不是传统的 try/catch block 。
一个忠告,不要混awaitthen .这是可能的,但很容易出错。坚持一个,可能await ,因为它更容易处理。

关于flutter - 如何处理已在库中声明的 dart 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64319402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com