如何使用flutter的.then()方法捕获错误?

问题描述 投票:0回答:1

我正在使用Fluttery geolocator插件在用户搜索同伴时将其转换为地址。但是,当使用无效的坐标时,将引发异常,但不会按预期方式处理(下面的代码)。

void placemarkFromCoordinates(latitude, longitude) async {
    Widget address;
    Geolocator().placemarkFromCoordinates(latitude, longitude).then((result) {
        print("result $result");
    }).catchError((error, stacktrace) {
        print(error);
    });

引发错误时,应打印错误。但是,catch块永远都不会到达,我只剩下一个例外:

发生异常。PlatformException(PlatformException(ERROR_GEOCODING_INVALID_COORDINATES,无法找到提供的坐标的地址。)

应该捕获此错误,但不是!

android asynchronous flutter dart future
1个回答
0
投票

[Geolocator flutter插件具有catchError方法,因此请使用它而不是]

© www.soinside.com 2019 - 2024. All rights reserved.